JavaScript provides three different ways to redirect the user to another web page:
1. If you assign a redirect URL to the location.href
property, then the user will be redirected to that URL. This process works as if a user has clicked a button. Let's understand the procedure with the help of an example:
Suppose you want to redirect the user to the homepage after entering his username and password. The simplest way is to assign the homepage URL to the href
property of the Location
object.
location.href = 'https://www.tutorialsandyou.com';
2. When you pass a redirect URL to the assign()
method of the Location
object, then the browser will take the user to the redirected URL.
Note: The assign()
method creates an entry in the browser's history. This means the user can simply navigate back to the original URL by clicking the back button.
location.assign('https://www.tutorialsandyou.com/javascript/');
3. The third way of redirecting the user to another web page is very interesting.
The Location
object has a method called replace()
, which also redirects users to another web page. The replace() method is different from the assign() method in the sense that it does not make an entry in the browser's history.
location.replace('https://www.tutorialsandyou.com/javascript/');