简体   繁体   English

等待网址更改点击事件

[英]wait for the url to change on click event

I have an image, on clicking the image the URL changes but the page is not reloaded(partial navigation). 我有一个图像,点击图像后URL更改但页面没有重新加载(部分导航)。 I have used window.location.href that fetches the current URL, but it displays the previous URL on console log. 我使用了window.location.href来获取当前的URL,但它在控制台日志中显示了以前的URL。 I want to fetch the URL after it changes. 我希望在更改后获取URL。

Am I missing some window wait event? 我错过了一些窗口等待事件吗?

To retrieve the new hash of the page, use location.hash : 要检索页面的新哈希,请使用location.hash

var hash = window.location.hash;

For a similar requirement in the past, I've used Ben Alman's hashChange plugin . 对于过去类似的要求,我使用了Ben Alman的hashChange插件 Once the plugin is included on page, you can attach code to the hashChange event: 一旦插件包含在页面上,您就可以将代码附加到hashChange事件:

$(window).hashchange( function(){
    // Your code here
})

Here's a working fiddle to demonstrate . 这是一个工作小提琴演示

Additional Information 附加信息

This SO post is worth reading: On - window.location.hash - change? 这个SO帖子值得一读: On - window.location.hash - 改变?

Note 注意

If you don't want to use a plugin, you'll have to post your markup before I can provide an alternative solution. 如果您不想使用插件,则必须在我提供替代解决方案之前发布您的标记。

var hash = window.location.hash;
var loc = window.location.href+hash;
window.location=loc;

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

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