简体   繁体   English

如何在不重新加载页面的情况下更改目标窗口的location.hash?

[英]How can I change the location.hash of a target window, without reloading the page?

I'm currently using one page to spawn a new window, with a timed playback URLs in it. 我目前正在使用一页来生成一个新窗口,其中包含定时播放的URL。 Many of the pages it's loading respond to hash navigation. 它正在加载的许多页面都响应哈希导航。 I'm doing this like: 我这样做是:

window.open(url,"playback");

As we play through the URLs, we should see the page respond accordingly. 在浏览URL时,我们应该看到页面做出相应的响应。 The problem I've run into however, is that the window.open() call actually reloads the page when the hash is changed. 但是,我遇到的问题是,更改哈希后,window.open()调用实际上会重新加载页面。

For instance, loading " pageA.htm ", then " pageB.htm#tab2 " works flawlessly. 例如,先加载“ pageA.htm ”,然后再加载“ pageB.htm#tab2 ”。 The issue however is that when I try to go from " pageB.htm#tab2 " to " pageB.htm#tab3 "; 但是问题是,当我尝试从“ pageB.htm#tab2 ”转到“ pageB.htm#tab3 ”时; the page reloads (responding properly to hash) completely instead of just firing "onhashchange" as I'd expect. 页面完全重新加载(正确响应哈希),而不是像我期望的那样仅触发“ onhashchange”。

Is there an alternative to window.open() that I should call for hash-only changes, that will prevent full page reload? 我应该调用window.open()的替代方法来进行仅哈希更改,以防止重新加载整个页面吗?

Edit: The final solution looks something like this: 编辑:最终解决方案如下所示:

playbackWindow = window.open(url,"playback");

Then when we want to change the hash: 然后,当我们要更改哈希值时:

playbackWindow.location.href = "poundIt";

You can't use window.open to change the hash without reloading the page. 如果不重新加载页面,则不能使用window.open更改哈希值。 Simply change the value of window.location.hash instead. 只需更改window.location.hash的值即可。

window.location.hash = "This"; 

应该可以。

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

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