Summary: In this tutorial, you will learn different ways to redirect an HTML page to another page or url in JavaScript.
There are several ways to redirect from one page to another using jQuery or JavaScript.
Here are three different methods that you can use:
1. Using the window.location object
The window.location object is a property of the window object that represents the current URL of the page. You can use the window.location object to get the current URL, to redirect to a new URL, or to open a new window or tab with a specified URL.
To redirect to a new URL using the window.location object, you can simply assign the new URL to the window.location object, like this:
// redirect to a new page
window.location = 'http://www.example.com';2. Using the location.replace() method
The location.replace() method is a method of the location object, which represents the current URL of the page. Like the window.location object, you can use the location.replace() method to redirect to a new URL or to open a new window or tab with a specified URL.
To redirect to a new URL using the location.replace() method, you can simply call the location.replace() method with the new URL as the argument, like this:
// redirect to a new page
location.replace('http://www.example.com');To open a new window or tab with a specified URL, you can use the window.open() method in conjunction with the location.replace() method, like this:
// redirect to a new page and specify the target attribute (e.g. "_blank")
window.open('http://www.example.com', '_blank').location.replace;3. Using the location.assign() method
The location.assign() method is also a method of the location object, which represents the current URL of the page. Like the window.location object and the location.replace() method, you can use the location.assign() method to redirect to a new URL or to open a new window or tab with a specified URL.
To redirect to a new URL using the location.assign() method, you can simply call the location.assign() method with the new URL as the argument, like this:
// redirect to a new page
location.assign('http://www.example.com');To open a new window or tab with a specified URL, you can use the window.open() method in conjunction with the location.assign() method, like this:
// redirect to a new page and specify the target attribute (e.g. "_blank")
window.open('http://www.example.com', '_blank').location.assign;