How to redirect page using Javascript or Jquery

Redirect Page using Javascript

There are multiple ways in JS to redirect page using javascript. The best way is to use window.location.href as it preserves the history when you hit the back button.Let us see multiple ways of implementing this in the below examples.

Example to redirect page using javascript using Javascript:

// The behavior of this method would be as HTTP redirect
window.location.replace("https://itsmycode.com");

// The behavior of this would be  as clicking on a link
window.location.href = "http://code.askemin.com";

//Navigate to the Location.reload article
window.location.assign("https://itsmycode.com");

All the three code does the same job. It would redirect to the URL, however, the Location.replace() method replaces the current resource with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session History, meaning the user won’t be able to use the back button to navigate to it.

Redirect Page using Jquery

There are multiple ways of redirecting to a page in Jquery. Let us take a look of each one.

Example using Jquery:

$(location).attr('href','https://itsmycode.com/');
$(window).attr('location','https://itsmycode.com')
$(location).prop('href', 'https://itsmycode.com')

If you are using Jquery to redirect to a page then you could use one of the above code for redirection.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like