How to redirect to another page using jQuery


Redirecting using Javascript

You don't need to redirect using jQuery, is not necessary you can use vanilla javascript. Using jquery is overkill.

One way is to use window.location.replace. The replace function is the recommended way to do a javascript redirect since it will not save the page history (HTTP_REFERRER) and this will avoid the back button redirect loop.


Redirect using jQuery

If you still want to use jQuery and not plain javascript you can use:


Some details on HTTP_REFERER when using javascript redirect

If you lose the HTTP_REFERER the user can't use the back-button. This is the desired effect when you want to do a redirect. You can also use window.location.href =., but this will allow the user to use the back button (which causes an infinite loop).

As a rule of thumb: * Use location.href: when you try to simulate an HTTP redirect. * Use location.replace. when you try to simulate a click

Full jquery redirect example

In this example we show a complete HTML with javascript code using jquery for a redirect:

<!DOCTYPE html>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
   $(document).ready( function() {
      $(location).attr("href", "https://tutorials.technology");

Javascript redirection en search engines

Search engines use the 301 status code to transfer the page rank from the old URL to the new URL. When you use javascript or jquery redirect the browser will get a 200 status code and this will affect the search engine. When doing SEO (Search engine optimization), you want to notify the search engines about your redirection you should add the rel="canonical" meta tag to your website. Adding a noscript section with an HTML refresh meta tag in it, is also a good solution.