Break when window.location changes?

JavascriptGoogle ChromeFirebugGoogle Chrome-Devtools

Javascript Problem Overview


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 hijacking 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:

['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?

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

Javascript Solutions


Solution 1 - Javascript

Network tab helps you, switch on preserve log checkbox, initiator column will contain the javascript location that caused the redirect.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPC JonesView Question on Stackoverflow
Solution 1 - JavascriptMartijnView Answer on Stackoverflow