I've got a navigation bars with five links and five divs. When I click the first link, the first div becomes visible and the rest are set to display: none
. The same goes for the rest of them.
I want to let the visitor bookmark these dynamic "pages" (the visible divs) so I'm using the URL hash. But it's not working as I expected. For instance, it's not possible to browse directly to one specific state (where a specific div is displayed). If I'm not being clear, what I want to accomplish is the same as in this video, although without making use of the next
and prev
methods.
Here's a sample of my code.
<style>
div { display:none; }
div#intro { display:block; }
</style>
<ul id="pages">
<li><a href="#intro">First link</a></li>
<li><a href="#continue">Second link</a></li>
<li><a href="#end">Third link</a></li>
</ul>
<div id="intro"></div>
<div id="continue"></div>
<div id="end"></div>>
<script>
function toggle() {
var i = 0,
divs = document.getElementsByTagName("div"),
hash = window.location.hash.substring(1); //extracts hash without #
for (i; i < divs.length ; i++) {
if (divs[i].id == hash) {
$(divs[i]).css("display", "block");
}
else {
$(divs[i].css("display", "none");
}
}
}
window.onhashchange = toggle;
</script>