Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

javascript - 开启-window.location.hash-更改了吗?(On - window.location.hash - Change?)

I am using Ajax and hash for navigation.(我正在使用Ajax和哈希进行导航。)

Is there a way to check if the window.location.hash changed like this?(有没有办法检查window.location.hash是否像这样更改?) http://example.com/blah #123 to http://example.com/blah #456(http://example.com/blah #123 http://example.com/blah #456) It works if I check it when the document loads.(如果我在加载文档时检查它,它将起作用。) But if I have #hash based navigation it doesn't work when I press the back button on the browser (so I jump from blah#456 to blah#123).(但是,如果我具有基于#hash的导航,则在按浏览器上的“后退”按钮时将无法使用(因此,我从blah#456跳至blah#123)。) It shows inside the address box, but I can't catch it with JavaScript.(它显示在地址框中,但我无法用JavaScript捕获它。)   ask by MilMike translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The only way to really do this (and is how the 'reallysimplehistory' does this), is by setting an interval that keeps checking the current hash, and comparing it against what it was before, we do this and let subscribers subscribe to a changed event that we fire if the hash changes.. its not perfect but browsers really don't support this event natively.(真正做到这一点的唯一方法(以及“ reallysimplehistory”如何做到这一点)是通过设置一个间隔来保持检查当前哈希值,并将其与之前的哈希值进行比较,然后执行此操作,并让订阅者订阅更改后的内容如果哈希发生变化,则触发该事件。它并不完美,但浏览器实际上并不原生支持此事件。)

Update to keep this answer fresh:(更新以使此答案保持最新:) If you are using jQuery (which today should be somewhat foundational for most) then a nice solution is to use the abstraction that jQuery gives you by using its events system to listen to hashchange events on the window object.(如果您使用的是jQuery(今天对于大多数人来说应该是基础的),那么一个不错的解决方案是使用jQuery通过使用其事件系统侦听window对象上的hashchange事件为您提供的抽象。) $(window).on('hashchange', function() { //.. work .. }); The nice thing here is you can write code that doesn't need to even worry about hashchange support, however you DO need to do some magic, in form of a somewhat lesser known jQuery feature jQuery special events .(这里的好处是您可以编写甚至不需要担心hashchange支持的代码,但是您确实需要做一些魔术,以一种鲜为人知的jQuery特性jQuery特殊事件的形式 。) With this feature you essentially get to run some setup code for any event, the first time somebody attempts to use the event in any way (such as binding to the event).(使用此功能,您基本上可以为任何事件运行一些设置代码,这是有人第一次尝试以任何方式(例如绑定到事件)使用该事件。) In this setup code you can check for native browser support and if the browser doesn't natively implement this, you can setup a single timer to poll for changes, and trigger the jQuery event.(在此设置代码中,您可以检查是否支持本机浏览器,如果浏览器不是本机实现的,则可以设置单个计时器以轮询更改并触发jQuery事件。) This completely unbinds your code from needing to understand this support problem, the implementation of a special event of this kind is trivial (to get a simple 98% working version), but why do that when somebody else has already .(这完全不需要您的代码来理解这个支持问题,这种特殊事件的实现是微不足道的(获得简单的98%工作版本),但是为什么别人已经这样做了呢?)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...