如何拦截window.location更改 [英] How to intercept window.location change

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

问题描述

我需要一种方法来为window.location的每个赋值附加一些预处理。有没有办法做到这一点。我基本上需要序列化一些将在新位置使用的信息。

I need a way to attach some pre-processing to each assignment of the window.location. Is there a way to do this. I basically need to serialize some info, that will be used in the new location.

推荐答案

你可以使用 beforeunload 事件触发代码运行在导航之前......如果您需要更具体的内容(例如,只在您的代码中启动导航),那么我建议您将代码更改为 window.location 进入一个单独的函数,并为你的自定义逻辑添加一个钩子。

You can use the beforeunload event to trigger your code to run prior to navigation... If you need something more specific (e.g., only navigations initiated in your code), then I recommend that you factor out code changing window.location into a separate function, and add a hook for your custom logic.

这篇关于如何拦截window.location更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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