The default browser behaviour when the hash address is changed is to jump to the id that represents the hash. For example: http://example.com/some-page#store
will search on the page the element with id store
(<div id="store">...</div>
). If this is found, the page will jump there.
Instead of setting the id attribute I suggest you to set a custom data-attr
attribute.
<div id="store">...</div>
will become <div data-page="store">...</div>
and in the jQuery side you will replace $(location.hash).show()
with:
$("[data-page='" + location.hash.substring(1) + "']").show()
The code will look like below:
$("a").on("click", function () {
// get the clicked link
var $clickedLink = $(this);
// get the url
var url = $clickedLink.attr("href");
// we search the hashes
if (url[0] !== "#") { return; }
// change the location using js
location.hash= url;
// hide all pages
$("[data-page]").hide();
// show the current page
$("[data-page='" + url.substring(1) + "']").show();
// prevent the default browser behaviour (jumping)
return false;
});