How to use (or is it possible) MutationObserver to monitor [removed].pathname change?

前端 未结 3 500
醉梦人生
醉梦人生 2021-01-12 14:52

I wonder if it is possible to use MutationObserver to monitor change in window.location.pathname (or window.location.hash).

3条回答
  •  旧时难觅i
    2021-01-12 15:49

    No - you cannot use MutationObservers

    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).

    Also, as other pointed out, window.location is a system object (of type Location Object) so it is not covered by Object.observe.

    You can test that with Chrome 43 which already supports Object.observe: kangax.github.io/compat-table/es7/#Object.observe

    So the only solution is to watch changes using timeout mechanism or use window.onpopstate (if you need to monitor only hash).

提交回复
热议问题