Skip to content Skip to sidebar Skip to footer

Redirect The Page And Prevent The Rest Of The Page From Loading And Script From Execution

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

Solution 1:

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

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

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

Solution 2:

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.

Solution 3:

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

Post a Comment for "Redirect The Page And Prevent The Rest Of The Page From Loading And Script From Execution"