What happens to code after a javascript redirect (setting window.location.href)?
Asked Answered
S

2

32

I have the following javascript redirect code followed by some more code.

window.location.href = '/someurl';
alert('hello');
alert('hello again');

This causes a browser inconsistency.

In firefox, the first alert is visible for a split second right before getting redirected. The second alert is not visible at all.

In chrome, both alerts will pop up, and the redirect happens only after clicking ok for both alerts.

Is there some concept of what happens to code after the redirect that can resolve these differences? If the redirect is asynchronous, then what is chrome doing? I would like to understand what is happening so that I can determine what to do in a more complicated scenario where a redirect is buried deep within some callbacks and other logic.

Suprematism answered 9/5, 2012 at 23:32 Comment(3)
Why would you want to do anything after a redirect?Photoactive
It will all depend on timing--once you change the location you can't rely on how long it will take to process/render/etc.Grewitz
@Photoactive Actually don't want to do anything after the redirect, which is why this question came up. For example, if the redirect is supposed to happen in a callback triggered from some other library's code, there could be other stuff that gets executed by the library after the library calls my callback. But if I just want to redirect the page without running that other stuff, I might need to do something to prevent it from continuing as normal, depending on what the browsers are trying to do.Suprematism
B
19

The browser will try to execute the code after window.location.href = 'url' until the page goes to the next web adress, so the number of lines of code that will be executed depends on the browser's speed

Belding answered 9/5, 2012 at 23:39 Comment(5)
Thanks, this seems plausible. I'm trying to test this by adding a long running loop after the redirect. If it was a matter of speed, wouldn't the browsers just redirect at random points during the loop? Both chrome and firefox seem to execute all the code no matter what the loop has in it (unless there's an alert in the loop, in which case firefox goes to the redirect at that point). I'm not sure if the long running loop test is the best test though...Suprematism
You are right. Maybe it's not the speed, but the priority of each javascript functionBelding
@DaniloValente do you have any resource to confirm thisBondstone
@johnny5 I just have my own experience as a "resource", but the MDN docs might be useful... Anyway, as Billbad stated, it's not safe to rely on code following window.location.Belding
Yeah I was scouring the spec for anything I ended up having to use a work aroundBondstone
R
31

The Javascript session will struggle to continue it's mission in the face of impending doom. Coding in this manner is considered unpredictable. ...and cruel.

Roney answered 10/5, 2012 at 0:34 Comment(0)
B
19

The browser will try to execute the code after window.location.href = 'url' until the page goes to the next web adress, so the number of lines of code that will be executed depends on the browser's speed

Belding answered 9/5, 2012 at 23:39 Comment(5)
Thanks, this seems plausible. I'm trying to test this by adding a long running loop after the redirect. If it was a matter of speed, wouldn't the browsers just redirect at random points during the loop? Both chrome and firefox seem to execute all the code no matter what the loop has in it (unless there's an alert in the loop, in which case firefox goes to the redirect at that point). I'm not sure if the long running loop test is the best test though...Suprematism
You are right. Maybe it's not the speed, but the priority of each javascript functionBelding
@DaniloValente do you have any resource to confirm thisBondstone
@johnny5 I just have my own experience as a "resource", but the MDN docs might be useful... Anyway, as Billbad stated, it's not safe to rely on code following window.location.Belding
Yeah I was scouring the spec for anything I ended up having to use a work aroundBondstone

© 2022 - 2024 — McMap. All rights reserved.