Here's a basic single page application structure using HTML, CSS, and JavaScript:
index.html
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My SPA</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="app">
<nav>
<ul>
<li><a href="#home" class="nav-link">Home</a></li>
<li><a href="#about" class="nav-link">About</a></li>
<li><a href="#contact" class="nav-link">Contact</a></li>
</ul>
</nav>
<main id="content">
<!-- Dynamic content will be loaded here -->
</main>
</div>
<script src="app.js"></script>
</body>
</html>
styles.css
css Copy code
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
line-height: 1.6;
}
nav {
background: #333;
padding: 1rem;
}
nav ul {
list-style: none;
display: flex;
gap: 2rem;
}
nav a {
color: white;
text-decoration: none;
padding: 0.5rem 1rem;
border-radius: 4px;
transition: background 0.3s;
}
nav a:hover, nav a.active {
background: #555;
}
#content {
padding: 2rem;
min-height: 80vh;
}
.page {
display: none;
}
.page.active {
display: block;
}
app.js
javascript Copy code
class SPA {
constructor() {
this.routes = {
'home': this.homePage,
'about': this.aboutPage,
'contact': this.contactPage
};
this.init();
}
init() {
// Handle navigation clicks
document.querySelectorAll('.nav-link').forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
const route = e.target.getAttribute('href').substring(1);
this.navigate(route);
});
});
// Handle browser back/forward buttons
window.addEventListener('popstate', () => {
const route = window.location.hash.substring(1) || 'home';
this.loadPage(route);
});
// Load initial page
const initialRoute = window.location.hash.substring(1) || 'home';
this.navigate(initialRoute);
}
navigate(route) {
window.history.pushState({}, '', `#${route}`);
this.loadPage(route);
}
loadPage(route) {
const content = document.getElementById('content');
// Update active nav link
document.querySelectorAll('.nav-link').forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${route}`) {
link.classList.add('active');
}
});
// Load page content
if (this.routes[route]) {
content.innerHTML = this.routes[route]();
} else {
content.innerHTML = this.notFoundPage();
}
}
homePage() {
return `
<div class="page active">
<h1>Welcome Home</h1>
<p>This is the home page of our single page application.</p>
<p>Navigate using the menu above to see different content load dynamically.</p>
</div>
`;
}
aboutPage() {
return `
<div class="page active">
<h1>About Us</h1>
<p>This is the about page.</p>
<p>Single page applications load content dynamically without full page refreshes.</p>
</div>
`;
}
contactPage() {
return `
<div class="page active">
<h1>Contact</h1>
<form id="contact-form">
<div style="margin-bottom: 1rem;">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" style="width: 100%; padding: 0.5rem;">
</div>
<div style="margin-bottom: 1rem;">
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" style="width: 100%; padding: 0.5rem;">
</div>
<div style="margin-bottom: 1rem;">
<label for="message">Message:</label><br>
<textarea id="message" name="message" rows="4" style="width: 100%; padding: 0.5rem;"></textarea>
</div>
<button type="submit" style="padding: 0.5rem 1rem; background: #333; color: white; border: none; cursor: pointer;">Send</button>
</form>
</div>
`;
}
notFoundPage() {
return `
<div class="page active">
<h1>404 - Page Not Found</h1>
<p>The page you're looking for doesn't exist.</p>
</div>
`;
}
}
// Initialize the SPA when the page loads
document.addEventListener('DOMContentLoaded', () => {
new SPA();
});
This basic SPA includes client-side routing, dynamic content loading, browser history support, and a simple navigation system. The JavaScript handles URL changes and updates the page content without full page reloads. You can extend this by adding more routes, implementing data fetching, or integrating with a framework like React or Vue for more complex applications.