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.

Post History

75%
+4 −0
Q&A JavaScript redirect is getting "hijacked" and it is not onbeforeunload event.

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

1 answer  ·  posted 1y ago by Vanity Slug ❤️‭  ·  last activity 1y ago by Vanity Slug ❤️‭

Question javascript redirect
#4: Nominated for promotion by user avatar Alexei‭ · 2023-08-09T04:41:13Z (over 1 year ago)
#3: Post edited by user avatar Vanity Slug ❤️‭ · 2023-08-08T15:49:34Z (over 1 year ago)
Add more debugging info.
  • 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.
  • 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** <small>(not the one that I need)</small> page happens.
#2: Post edited by user avatar Vanity Slug ❤️‭ · 2023-08-07T15:29:41Z (over 1 year ago)
Update with more details..
  • 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?**
  • 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.
#1: Initial revision by user avatar Vanity Slug ❤️‭ · 2023-08-07T13:39:08Z (over 1 year ago)
JavaScript redirect is getting "hijacked" and it is not onbeforeunload event.
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?**