-1

My redirect in javascript somehow gets "hijacked" and I don't understand how.

Previous developer (who is no longer working with the company) on page load initialized onbeforeunload event like so:

window.onbeforeunload = function() {
    if(<some condition>)
        <make some ajax request>
}

Now I have a requirement where I need to do a redirect, like so:

window.location = "https://duckduckgo.com"; // just some external url

Both pieces of code above are being reused by multiple pages. In all pages redirect works, except one. Only on 1 page after my debugger steps over my redirect and then reaches this point

    window.onbeforeunload = function() {
        if(<some condition>)
            <make some ajax request>
--> }

I do a step over and instead of getting redirected to https://duckduckgo.com I get redirected to a different internal page in my application.

Looks like something is happening in javascript between onbeforeunload and my redirect but I can't understand what it is. I don't even understand how I can continue debugging this.

- How is that possible?

- What can I do to debug this farther?

- Is there some kind of onbeforebeforeunload event or something?


Update.

"onbeforeunload" and "redirect" snippets of code are located in two different files, kind of like so:

File A.js:

$(function(){
    ...
    window.onbeforeunload = function() {
        if(<some condition>)
            <make some ajax request>
    }
    ...
});

File B.js

var Obj1 = (function(){
    return {
        init(): function() {
            this.Obj2.init();
        }
    }
});

Obj1.Obj2 = (function(){
    var process = function(){
        ...
        window.location = "https://duckduckgo.com";
        ...
    }
    return {
        run: process,
        init: function(){
            ...
        }
    }
});

and then at some point process function gets called:

Obj1.Obj2.run();

Update 2:

I put debugger; in and out of every single piece of code where I saw "beforeunload" like so in my entire project:

$(this).on("beforeunload", function(){
    ...
});

Not a single breakpoint got hit.


Update 3.

I added the following lines of code prior to redirect:

window.onunload = function(){
    debugger; // Not hit
};
window.onbeforeunload = function(){
    debugger; // <-- Hit 1st
};
$(window).on("beforeunload", function() {
    debugger; // <-- Hit 2nd
});
$(window).on("unload", function() {
    debugger; // Not hit
});
window.location = "https://<url>";

As comments above describe, first window.onbeforeunload gets hit, then $(window).on("unload"... and then redirect to internal (not the one that I need) page happens.

  • Can you show how your redirect is implemented then? Because yes, that should work but are you doing this on the before unload and are you doing it inside the method defined by the old dev? – somethinghere Aug 07 '23 at 13:48
  • 1
    The code is probably listening to the `beforeunload` event and not using the property `onbeforeunload`. – evolutionxbox Aug 07 '23 at 13:49
  • @somethinghere "Can you show how your redirect is implemented then?" I am not sure what can I show there that would be relevant or would help to understand the problem better. I just have a function that has another function in it that has another function that has that redirect line of code in it. Is there something specific you are looking for? – Vanity Slug - codidact.com Aug 07 '23 at 14:32
  • @somethinghere "are you doing it inside the method defined by the old dev" in the context you are asking the answer is probably no. onbeforeunload is happening in one file, redirect happens in it own function scope inside of another file. – Vanity Slug - codidact.com Aug 07 '23 at 14:34
  • Yes, where is your call happening? As @evolutionxbox said, if you add it as an event listeners it makes a difference compared to setting it to `window.onbeforeunload` etc.... We know how to redirect, we don't know anything about how you get to that piece of code, so there's no way for us to help you figure out what is preventing your redirect and executing the old code. We jsut don't know _how_ your redirect is even hooked up... – somethinghere Aug 07 '23 at 14:35
  • @evolutionxbox I looked for "beforeunload". I have a few `$(this).on("beforeunload", function(){...})` snippets of code but so far non that I found seem to be relevant. Non of those that I tested hit my breakpoints. I will looking. – Vanity Slug - codidact.com Aug 07 '23 at 15:15
  • @somethinghere "we don't know anything about how you get to that piece of code" I see. Indeed. I have thousands of lines of messy js code to read through to understand that myself. It's not a good question for stackoverflow, but you guys still pointed me in the right direction. Thank you. – Vanity Slug - codidact.com Aug 07 '23 at 15:55
  • [This answer](https://stackoverflow.com/questions/9298839/is-it-possible-to-stop-javascript-execution#answer-24582915) helped me to achieve redirect that I want. It's not the answer I deserve, but it is the answer that I need right now. – Vanity Slug - codidact.com Aug 08 '23 at 16:13

0 Answers0