Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload page when changing hash

Reload page when changing hash.

I have a simple one-page site with several 'pages' on it. These pages are in one wide container which scrolls when you choose one. I have added a hash to the URL so that you can locate to specific pages directly. This just sets the style.left attribute when it matches a hash in a switch statement.

The problem is that when I change the hash value in the URL. For example, changing it from Home.html#Web to Home.html#Photos. When doing this, the page doesn't reload so the Setup function I've created that check for the hash isn't called and the page just remains where it is.

Any ideas for a way to force the page to reload? Or a better solution?

Thanks, Andy

like image 919
andyfurniss Avatar asked Sep 12 '13 21:09

andyfurniss


1 Answers

Don't reload the page. Instead, set up a onhashchange event handler, and adjust the left value from there:

window.onhashchange = function() {
    // do stuff
}

Otherwise, why using hash links instead of regular links? In any case, if you really want to reload, just put window.location.reload() inside the onhashchange handler...

like image 187
bfavaretto Avatar answered Sep 22 '22 00:09

bfavaretto