1

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?

Niko
  • 251
  • 3
  • 6
  • 17

2 Answers2

1

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.

MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
0

Depending on your requirement you can as well use onhashchange event

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

Browser support

sabithpocker
  • 15,274
  • 1
  • 42
  • 75