You can redirect to another webpage in JavaScript by setting the window.location property to the URL of the destination. Here's an example:
// Redirect to another webpage
window.location.href = "https://www.example.com";
This line of code changes the current location of the browser to the specified URL, effectively redirecting the user to the new page.
You can also use window.location.replace() for a similar effect:
// Redirect using replace
window.location.replace("https://www.example.com");
The difference between window.location.href and window.location.replace() is that the latter replaces the current page in the browser's history, making it so that the user cannot navigate back to the previous page using the browser's back button.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
You can redirect to another webpage in JavaScript by setting the window.location property to the URL of the destination. Here's an example:
This line of code changes the current location of the browser to the specified URL, effectively redirecting the user to the new page.
You can also use window.location.replace() for a similar effect:
The difference between window.location.href and window.location.replace() is that the latter replaces the current page in the browser's history, making it so that the user cannot navigate back to the previous page using the browser's back button.