If I use a server-side redirect in ASP.NET code, I know that it will raise a ThreadAbortException and so all processing on the page will immediately stop and the redirect will happen straight away (after "finally" clauses).
However, Say I do a redirect inside a JavaScript block (by setting location.href or using location.replace), when does this redirect actually take effect?
Reloading the page would obviously unload the current script block but based on a quick test, an alert statement after the redirect still works. Does the redirect take place only once processing of the current event has completed? More importantly is this behaviour standardized, or just a de-facto convention?
OK I've done some more of my own tests. Jerebear is right as far as he goes in that if you have a redirect that happens directly inside a script tag (i.e. the redirect is executed when the script tag is loaded) then this stops loading any further script tags on the same page. so if you have something like:
<script> a(); redirect(); b(); </script>
<script> c(); </script>
then a and b would be executed but not c, because the second script would never be loaded.
However, this is only part of the story. I was really thinking about what happens when you redirect as a result of an event (click, timer etc). The answer to this is that doing a redirect does not stop events being processed on the page at all. This means all events continue to work normally until the new page starts to load.
So the general answer is that the redirect doesn't really take effect until the new page is received from the server, although it does have the side effect of not loading any more script tags (and possibly not loading other resources) on the current page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With