简体   繁体   English

当仅URL哈希值更改时,如何重新加载整个页面?

[英]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? 如果唯一的URL更改是哈希,如何重新加载页面? location.href doesn't work as I expect. location.href无法正常运行。

location.href= siteUrl + '#T_blockName';

This doesn't refresh the page unless I change siteUrl . 除非我更改siteUrl否则不会刷新页面。 How is it possible? 这怎么可能?

You need to use location.hash . 您需要使用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. 要更改它,您需要使用锚点或将window.location设置为该位置以及所需的哈希值。

Also, you are using PHP concatenation syntax when you should be using + . 另外,应在使用+时使用PHP串联语法。

That is not how you concatenate strings in javascript, you need to use + instead of . 这不是在JavaScript中串联字符串的方式,您需要使用+而不是. .

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.). 可能的原因是,仅更改哈希部分不会请求重新加载页面,因为通常认为哈希是页面的一部分(锚,ID等)。

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. 您需要专门请求页面重新加载,但是如何进行重新加载取决于您实际想要实现的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅在URL中的参数更改时重新加载页面 - Reload page when only param in URL changes 网址哈希更改时强制重新加载 - Force reload when url hash changes 当AJAX在不重新加载页面的情况下更改URL时,如何重新加载Greasemonkey脚本? - How do I reload a Greasemonkey script when AJAX changes the URL without reloading the page? 如何在不重新加载整个列表的情况下在我的组件 CoinRow 中侦听此变量“编辑”的更改? - How can I listen for changes to this variable 'editing' in my component CoinRow without reload the entire list? 如何使 function 仅在使用 angular 重新加载页面时运行? - How can I make a function run only when I reload the page with angular? JavaScript:本地存储更改时如何重新加载页面? - JavaScript: How to reload page when localstorage changes? 如何编辑URL的一部分并重新加载新页面 - How can I edit a portion of a URL and reload the new page 如何在不重新加载页面的情况下向我的 url 添加参数? - How can I add a parameter to my url, without reload the page? 如何在URL中添加参数,然后使用Greasemonkey重新加载页面? - How can I add a parameter to a URL and then reload the page using Greasemonkey? 如何在不重新加载页面的情况下获取URL的新哈希值 - How to get new hash value of URL without page reload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM