如何检测JavaScript中的URL更改 [英] How to detect URL change in JavaScript

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

问题描述

如何检查JavaScript中的网址是否已更改?例如,使用AJAX的GitHub等网站会在#符号后附加页面信息,以创建唯一的URL而无需重新加载页面。检测此URL是否更改的最佳方法是什么?

How can I 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的事件处理程序?

  • 或者必须每秒检查一次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.

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

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