如何在JavaScript中哈希后检测URL是否已更改 [英] How to detect if URL has changed after hash in JavaScript

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

问题描述

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




  • 正在加载事件再次被调用吗?

  • URL是否有事件处理程序?

  • 还是必须每秒检查一次URL以检测更改?

解决方案

在现代浏览器(IE8 +,FF3.6 +,Chrome)中,您只能听在窗口上的 hashchange 事件。



在某些旧版本中浏览器,您需要一个计时器来连续检查 location.hash 。如果您使用的是jQuery,则可以使用插件来实现。 p>

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?

  • 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?

解决方案

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

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天全站免登陆