published: 29 May 2022
2 min read
How to redirect to another web page with JavaScript
In JavaScript, The assign()
method of the Location
object can be used to navigate to a new web page.
Here is an example:
location.assign('https://attacomsian.com/protips');
The replace()
method is another way to load a new web page in JavaScript. It works similar to assign()
except that it simulates an HTTP redirect and doesn't create an entry in the browser's history. It means that the user won't be able to use the back button to navigate back to the original document:
location.replace('https://attacomsian.com/protips');
Finally, the last way to redirect the user to another web page is by using the location.href
property. It resembles the user's click event and produces the same effect as using the assign()
method:
location.href = 'https://attacomsian.com';
Take a look at JavaScript location object tutorial to learn more about the Location
object properties and methods.
Are we missing something? Help us improve this article. Reach out to us.
How to redirect to another web page with JavaScript
In JavaScript, The assign()
method of the Location
object can be used to navigate to a new web page.
Here is an example:
location.assign('https://attacomsian.com/protips');
The replace()
method is another way to load a new web page in JavaScript. It works similar to assign()
except that it simulates an HTTP redirect and doesn't create an entry in the browser's history. It means that the user won't be able to use the back button to navigate back to the original document:
location.replace('https://attacomsian.com/protips');
Finally, the last way to redirect the user to another web page is by using the location.href
property. It resembles the user's click event and produces the same effect as using the assign()
method:
location.href = 'https://attacomsian.com';
Take a look at JavaScript location object tutorial to learn more about the Location
object properties and methods.
Are you looking for other code tips?
JS Nooby
Javascript connoisseur