如何检测URL变化 [英] How to detect url change

查看:511
本文介绍了如何检测URL变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是检查如果URL中的JavaScript已经改变的最佳途径。例如,像Github上的网站其使用AJAX#符号后,将追加页面信息来创建一个唯一的URL无需刷新页面。什么是检测的最佳方式,如果该URL的变化?

What is the best way to check if a url has changed in JavaScript. For example, websites like Github which use AJAX will append page information after a # symbol to create a unique url without reloading the page. What is the best way to detect if this URL changes?

时再次呼吁onload事件? 是否有一个事件处理程序的网址是什么? 还是必须的URL进行检查每一秒检测的变化?

Is the onload event called again? Is there an event handler for the url? Or must the url be checked every second to detect a change?

推荐答案

在现代浏览器(IE8 +,FF3.6 +,Chrome浏览器),你可以听 hashchange 活动的窗口

In modern browsers(IE8+, FF3.6+, Chrome), you can just listen to the hashchange event on window.

在一些旧的浏览器,你需要一个计时器,不断检查的location.hash 。如果你正在使用jQuery,有一个插件这正是这么做的。

In some old browsers, you need a timer that continually checks location.hash. If you're using jQuery, there is a plugin that does exactly that.

这篇关于如何检测URL变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆