Hi I am looking for a very simple example of single page web application implimented with pure javascript/jquery. Don't want to use angular backbone ember etc..
-
possible duplicate of Architecture for single page application (JavaScript)Barmak Shemirani– Barmak Shemirani2015-06-21 05:37:30 +00:00Commented Jun 21, 2015 at 5:37
-
3Anybody has answer to this question ?? or just want to mark this duplicate... ?? anybody ?shikhar chauhan– shikhar chauhan2015-06-21 05:44:37 +00:00Commented Jun 21, 2015 at 5:44
-
3have you tried google? the web is full of simple examples like thesesvarog– svarog2015-06-21 05:48:59 +00:00Commented Jun 21, 2015 at 5:48
-
2@svarog did you try understandt or even read my question.. please try understanding what I wanted. any very simple example of hash linking implementation.shikhar chauhan– shikhar chauhan2015-06-21 06:33:45 +00:00Commented Jun 21, 2015 at 6:33
-
2@shikharchauhan you never mentioned hash linking. The question asks for an example of a web page using JavaScript...well you can get that anywhere. You didn't say what features it should have, and even if you did, you can research them yourself. That's why the question is closed. The question might be "simple" but it's also massively open-ended and completely unsuitable for answering in the SO style.ADyson– ADyson2017-08-29 12:13:51 +00:00Commented Aug 29, 2017 at 12:13
2 Answers
2021 Update:
It's nearing the end of 2021 and thought I would provide a newer solution/guide if possible. I did end up finding one, created a couple years ago, that uses modern day tools (npm, webpack, ES6, etc.):
However, I reviewed the original three guides below and TBH they're still relevant 6 years later! Which one you should choose solely depends on what suits your needs best.
Here are some links to help point you in the right direction. If you don't want to use any frameworks (Angular, Backbone, etc.) these links will be good. They use jQuery and perhaps one other library/plugin. Hope they help!
Guide A - uses jQuery library handlebars
2 Comments
You can achieve a simple SPA by dynamically updating the content based on the hash-based routing (window.location.hash). Below is a basic example:
// Define app routes
const routes = {
"#home": "<h2>🏠 Home Page</h2><p>Welcome to our single-page application.</p>",
"#about": "<h2>ℹ️ About Us</h2><p>We are a simple SPA built with JavaScript and jQuery.</p>",
"#contact": "<h2>📞 Contact</h2><p>Get in touch via email at [email protected].</p>"
};
function loadPage() {
let hash = window.location.hash || "#home"; // Default to home
$("#app").html(routes[hash] || "<h2>404 Not Found</h2>");
}
$(document).ready(function() {
loadPage(); // Load initial view
$(window).on("hashchange", loadPage); // Listen for hash changes
});
body { font-family: Arial, sans-serif; }
nav a { margin: 10px; cursor: pointer; color: blue; text-decoration: underline; }
.content { margin-top: 20px; }
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple SPA with JavaScript & jQuery</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<!-- Navigation Menu -->
<nav>
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</nav>
<!-- Dynamic Content Area -->
<div class="content" id="app"></div>
</body>
</html>
This code is easy to understand and should help you create a simple single-page web application.