Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
41 lines (37 loc) · 1.5 KB
/
Copy pathapp.js
File metadata and controls
41 lines (37 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// httpx.zig SPA Example - Client-side JavaScript
document.addEventListener('DOMContentLoaded', function() {
// Fetch button handler
const fetchBtn = document.getElementById('fetch-btn');
const output = document.getElementById('output');
if (fetchBtn) {
fetchBtn.addEventListener('click', async function() {
output.textContent = 'Loading...';
try {
const response = await fetch('/static/index.html');
const text = await response.text();
output.textContent = 'Response length: ' + text.length + ' bytes\nStatus: ' + response.status;
} catch (err) {
output.textContent = 'Error: ' + err.message;
}
});
}
// Contact form handler
const contactForm = document.getElementById('contact-form');
const formOutput = document.getElementById('form-output');
if (contactForm) {
contactForm.addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(contactForm);
const data = Object.fromEntries(formData.entries());
formOutput.textContent = 'Form submitted:\n' + JSON.stringify(data, null, 2);
contactForm.reset();
});
}
// Active nav link
const path = window.location.pathname;
document.querySelectorAll('.nav-link').forEach(function(link) {
if (link.getAttribute('href') === path) {
link.classList.add('active');
}
});
});