Javascript window.location 改变时中断?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11194971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Break when window.location changes?
提问by PC Jones
I've got a page that's redirecting when it shouldn't be, and I'm trying to figure out who's doing it. First I tried hiHymaning 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));
That didn't work at all in Chrome. You can't do setters and getters on window.location for security reasons. OK. The next thing I tried was observing onunload and onbeforeunload:
这在 Chrome 中根本不起作用。出于安全原因,您不能在 window.location 上执行 setter 和 getter。好的。我尝试的下一件事是观察 onunload 和 onbeforeunload:
['unload', 'beforeunload'].forEach(function (evName) {
window.addEventListener(evName, function () {
debugger; // Chance to check everything right before the redirect occurs
});
});
I knew that the value of window.location would stay the same until after the onunload event, again for security reasons, but I was hoping for some other clue; no such luck. I next tried putting breakpoints at every point I can find in my own scripts where window.location could possibly be assigned. None of them are hit according to the Chrome debugger. Argh. This redirect is not happening in FF, by the way, and I already tried restarting Chrome. I feel like I've truly tried everything and gotten nowhere, which hopefully means I'm about to level up as a developer? Please?
我知道 window.location 的值会保持不变,直到 onunload 事件之后,再次出于安全原因,但我希望有其他线索;没有这样的运气。接下来,我尝试在我自己的脚本中可以找到的每个可能分配 window.location 的点上放置断点。根据 Chrome 调试器,它们都没有被命中。啊。顺便说一下,这种重定向在 FF 中没有发生,我已经尝试重新启动 Chrome。我觉得我真的尝试了所有方法但一无所获,这是否意味着我即将升级为开发人员?请?
Is there any way in any browser for me, the human operating the debugger, to break on the line that's redirecting the page location? I understand there are security implications for allowing automatic access to that information, but is there nothing that privileges the developer and allows a way to do it? If not, what's the normal way of dealing with a situation like this? It seems common enough and I don't think anyone likes getting blocked for hours or days on a conceptually simple problem. TIA
对我来说,在任何浏览器中,操作调试器的人有没有办法在重定向页面位置的行上中断?我知道允许自动访问该信息存在安全隐患,但是没有什么可以授予开发人员特权并允许这样做吗?如果不是,处理这种情况的正常方法是什么?这似乎很常见,我认为没有人喜欢在概念上很简单的问题上被阻止数小时或数天。TIA
回答by Martijn
Network tab helps you, switch on preserve log checkbox, initiator column will contain the javascript location that caused the redirect.
网络选项卡可以帮助您,打开保留日志复选框,启动器列将包含导致重定向的 javascript 位置。