By using this site you agree to the use of cookies.

Learn more

Share via:

JavaScript Redirect

To perform a client-sided redirect with JavaScript we can use the window.location object. Learn more in this tutorial.

Edited: 2017-06-19 14:25

To make a JavaScript Redirect you need to use the window.location object, this is done by simply feeding it with the URL that you want to redirect to. But there are more to redirects then what can be seen on the client side, the important part is often the part happening on the server – those seeking to use JavaScript for redirects, should note that you can not send header responses with JavaScript, so your redirect may not have the desired effect for search engines.

The window.location object can be used whenever you want to send a client sided redirect, this will often strictly be done when you can not use server-sided scripting. The below code can be placed in your head section.


And as a reminder, here is the full code, ready to be pasted in the head section of your HTML.

<script type="text/javascript">

The Disadvantages of a JavaScript Redirect

The JavaScript redirect is considered dirty in most situations – there likely are times where it would be fine and very suitable – but the majority of web developers likely never encounter them. If you can't see a reason to use it, then you likely shouldn't be using it – the best advice is to stick with server-sided redirects unless you really need to use JavaScript.

JavaScript based redirects will not pass PageRank, and it will most likely not have the desired effect in search engines. Search engines look at the header response of the requested resource, and the header response is delivered from the server-side.

It is considered safer to rely on server-sided redirects for now – if you for some reason can not use server-sided redirects, you may want to consider using a meta refresh redirect instead.

See also

  1. Redirecting pages with htaccess
  2. Meta Tag Redirects
  3. Canonical Tag