2

If we have such code:

window.location = 'http://example.net';
$.ajax({...});

Is any chance that the ajax code will be sent? Or there is no chance, that ajax request will be ever executed? thank you

Mihey Mik
  • 1,643
  • 13
  • 18
  • 1
    No, there is chance. – Scott Marcus May 10 '17 at 14:37
  • 1
    I wouldn't count on that ajax request being completed successfully (if at all). – Lix May 10 '17 at 14:37
  • 2
    My suggestion would be to move `window.location` into the `onSuccess` callback of the ajax call - that way you'll only redirect users *after* the ajax request has finished. – Lix May 10 '17 at 14:37

3 Answers3

2

When the location property is altered, the user agent begins to unload the current document in preparation for the loading of the new one. Only code that is registered into the document or window beforeunload or unload events will execute at that point.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
2

The browser will try to execute the code after window.location 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

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

Community
  • 1
  • 1
Mike Adamenko
  • 2,944
  • 1
  • 15
  • 28
-2

no. js will not execte any line of code after get the redirect order.

farbod
  • 91
  • 7