小编PC *_*nes的帖子

window.location改变时中断?

我有一个页面,当它不应该重定向时,我正在试图找出谁在做它.首先我试着劫持window.location:

window.location = (function (location) {
    // location is now hidden inside a closure. We can override it
    var Location = Object.create(location);
    // Location is now our proxy. We can use it to catch changes
    window.__defineGetter__('location', function () { return Location });
    window.__defineSetter__('location', function (x) { debugger; location = x; return x; });
    // etc etc, considered assignments to location.href, location.search, location.host etc., as well as calls to location.replace and location.assign
}(window.location));
Run Code Online (Sandbox Code Playgroud)

这在Chrome中根本不起作用.出于安全原因,您无法在window.location上执行setter和getter.好.我尝试的下一件事是观察onunload和onbeforeunload:

['unload', 'beforeunload'].forEach(function (evName) {
    window.addEventListener(evName, function () …
Run Code Online (Sandbox Code Playgroud)

javascript firebug google-chrome google-chrome-devtools

66
推荐指数
1
解决办法
3万
查看次数