How to Redirect to Another Web Page Using Javascript

This quick post shows how to redirect to another webpage using JavaScript.

How to Redirect to Another Webpage Using Javascript

There are a couple of ways to redirect to another webpage with JavaScript. The most popular ones are location.href and location.replace:
// Simulate a mouse click:
window.location.href = "";

// Simulate an HTTP redirect:
If you want to simulate someone clicking on a link, use location.href
If you want to simulate an HTTP redirect, use location.replace
The simplest way to test this snippet, open a console (which, in Chrome and Firefox, can be done by pressing Ctrl+Shift+I), navigate to the "console" tab, copy-and-paste each JavaScript code example from this post, and run it by pressing the Enter/Return key.
There are lots of ways of doing this.
// window.location
window.location.href = ''
document.location.href = '/path'

// window.history

// window.navigate; ONLY for old versions of Internet Explorer

// Probably no bueno
self.location = '';
top.location = '';

// jQuery
$(location).prop('href', '')


Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course