4

I need to redirect page when it starts loading (e.g. the head part of the page loaded) and once it is redirected prevent/stop the rest of the page content from loading and the rest of the script from execution.

If I place this code (window.location = "/other-page-url";) at the first line after opening HEAD tag and redirect page to another one, will it stop execution of next going scripts? I want to be sure that if there are, let's say, some counter script called on the second line, just after redirection, it will not execute.

May be JavaScript has some method which stops page loading/rendering?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mra214
  • 509
  • 1
  • 7
  • 18
  • Instead of initiating a redirect at the client's side, I recommend to send a `Location: ...` header, to request a redirect even before the page has been rendered. – Rob W Jan 06 '12 at 15:05

3 Answers3

0

I tested this extensively in Chrome on Windows, taking notes from the discussion at https://stackoverflow.com/a/24070373/5749914. To expound on Joseph Silbur's answer:

Once you have redirected the user to a different page, the current page will stop execution immediately.

JavaScript within the head tag is executed before any script tags in the body of the document. Although Chrome make requests for script tags within the body tag, those scripts are not executed. The only code that is executed is JavaScript within the head tag.

Also of note, Chrome fires the DOMContentLoaded event before redirecting the page. This doesn't matter unless you have something listening specifically to that event.

So yeah, window.location = "/other-page-url" works, so long as it's the only code in the head tag.

Elliott Beach
  • 10,459
  • 9
  • 28
  • 41
0

You can do this before any Javascript is executed using a <meta> tag in your <head> before any <script> tags:

<meta http-equiv="Refresh" content="0; url=http://www.example.com/">

This would redirect the browser to example.com as soon as it encounters the tag.

NobRuked
  • 593
  • 4
  • 12
-3

Once you have redirected the user to a different page, the current page will stop execution immediately.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • Does it mean that user will not notice any changes on the page itself except URL change in the address bar? I mean will they see the rendering of the page if computer or internet connection is slow? – mra214 Jan 06 '12 at 15:13
  • @mra214 - Correct. The user won't see anything rendered on the page. – Joseph Silber Jan 06 '12 at 15:18
  • 9
    I ran into the same issue, and adding some tracing to console.log right after the window.location = "/new_url" shows that script execution does *not* stop after the redirect. I was on chrome, on OSX and haven't tested this on other browsers, but at least it proves that the behavior is not consistent across all browsers. – Shyam Habarakada Apr 18 '12 at 14:49
  • 2
    I found that this is not exactly true. If I put an alert, right after the window.location it is still triggered, so I assume it has a small delay before the page stops executing and redirects. – nxtwrld Aug 15 '13 at 13:48
  • 3
    This a bad answer. If you are in a callback and you redirect, the code will still execute in the main function and only after that the redirect will happen. – Sorin Trimbitas Aug 04 '14 at 13:17
  • @SorinTrimbitas "**If I place this code ... *at the first line after opening HEAD tag* and redirect page to another one, will it stop execution of next going scripts?**" Answer: **yes**. – Joseph Silber Aug 04 '14 at 15:58