Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on JavaScript redirect is getting "hijacked" and it is not onbeforeunload event.

Post

JavaScript redirect is getting "hijacked" and it is not onbeforeunload event.

+4
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

Probably is too late, but you might try looking in the "Initiator" column for the request in the "Net... (1 comment)
It seems like you're looking for something that happens *after* `onbeforeunload`, not before. Wouldn'... (3 comments)
Probably is too late, but you might try looking in the "Initiator" column for the request in the "Net...
alagos‭ wrote over 1 year ago · edited over 1 year ago

Probably is too late, but you might try looking in the "Initiator" column for the request in the "Network" tab of a Chrome console and it will show you where that request was triggered from. Like here https://i.imgur.com/Kc0Mv6Q.png