如何使用(或可能)使用MutationObserver监视window.location.pathname的更改? [英] How to use (or is it possible) MutationObserver to monitor window.location.pathname change?
问题描述
我想知道是否可以使用MutationObserver监视window.location.pathname(或window.location.hash)中的更改.
I wonder if it is possible to use MutationObserver to monitor change in window.location.pathname (or window.location.hash).
推荐答案
否-您不能使用MutationObservers
No - you cannot use MutationObservers
新的EcmaScript 7(预览,草稿)将具有 Object.observe
,它将允许您监视任何对象.但是,这就是无法使用:观察全局对象存在安全风险,我怀疑是否有任何浏览器会允许这样做(
The new EcmaScript 7 (in preview, draft) is going to have Object.observe
which will allow you to monitor any object. However, that is in not going to work: observing global objects is a security risk and I doubt that any browser will allow that (Chromium issue 494574).
此外,正如其他人指出的那样,window.location是一个系统对象(属于Location Object类型),因此它不在 Object.observe
范围内.
Also, as other pointed out, window.location is a system object (of type Location Object) so it is not covered by Object.observe
.
您可以使用已经支持Object.observe的Chrome 43进行测试: kangax.github.io/compat-table/es7/#Object.observe
You can test that with Chrome 43 which already supports Object.observe: kangax.github.io/compat-table/es7/#Object.observe
因此,唯一的解决方案是使用超时机制或使用 window.onpopstate
(如果仅需要监视哈希)来监视更改.
So the only solution is to watch changes using timeout mechanism or use window.onpopstate
(if you need to monitor only hash).
这篇关于如何使用(或可能)使用MutationObserver监视window.location.pathname的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!