简体   繁体   中英

How can I reload the entire page when only the url hash changes?

How do I reload a page if the only change to the url is a hash? location.href doesn't work as I expect.

location.href= siteUrl + '#T_blockName';

This doesn't refresh the page unless I change siteUrl . How is it possible?

You need to use location.hash . This retrieves the hash. To change it, you need to use an anchor or set window.location to the location plus the hash you want.

Also, you are using PHP concatenation syntax when you should be using + .

That is not how you concatenate strings in javascript, you need to use + instead of . .

location.href= CURRENT_SITE_URL + '#T_blockName';

Its possible because simply changing the hash part isn't going to request a page reload since hash is usually considered to be part of the page (anchor, id etc.).

You need to specifically request a page reload but how you are going to do it depends on what you are actually trying to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM