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.

user1385729
  • 1,276
  • 1
  • 15
  • 19
  • 1
    Why would you want to do anything after a redirect? – Musa May 09 '12 at 23:35
  • 1
    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. – Dave Newton May 09 '12 at 23:37
  • 1
    @Musa 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. – user1385729 May 10 '12 at 01:38

2 Answers2

30

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.

Billbad
  • 17,651
  • 2
  • 21
  • 17
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

Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
  • 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... – user1385729 May 10 '12 at 01:17
  • You are right. Maybe it's not the speed, but the priority of each javascript function – Danilo Valente May 10 '12 at 01:19
  • @DaniloValente do you have any resource to confirm this – johnny 5 Jun 27 '17 at 16:12
  • 1
    @johnny5 I just have my own experience as a "resource", but the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/API/Window/location) might be useful... Anyway, as Billbad stated, it's not safe to rely on code following `window.location`. – Danilo Valente Jun 28 '17 at 11:44
  • Yeah I was scouring the spec for anything I ended up having to use a work around – johnny 5 Jun 28 '17 at 12:33