Niko
Niko

Reputation: 251

How do I reload a page with an additional hash using javascript?

I want to refresh the page with an additional hash in the url. The problem is that the hash is added but the page is not reloaded which I want in this case. I have tried the following:

window.location.href = "http://www.mydomain.com/page1#test";

The hash is added in the url but the page is not reloaded. How do I achieve this functionality?

Upvotes: 1

Views: 119

Answers (2)

sabithpocker
sabithpocker

Reputation: 15566

Depending on your requirement you can as well use onhashchange event

window.onhashchange = function(){
    if (location.hash === "#test") {
        test();
    }
}

Browser support

Upvotes: 0

MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29176

Try -

location.reload(true);

after you've changed the url -

window.location.href = "http://www.mydomain.com/page1#test";
location.reload(true);

Check out this example from MDN.

Upvotes: 1

Related Questions