It's quite common to redirect a user visiting your webpage with javascript location href, more specifically when they perform an action, such as: clicking a button. When the user completes the action and you want to redirect the user to somewhere else, you have two common choices: window.location vs window.location.href
OR Either of these JavaScript functions will redirect the user to the specified page by using the Javascript string replace() method, but what's the difference? With the current document only one function will update the browser's document history When using location.href the current URL will be added to the user's navigation history. This means if they click the Back button, they will return to the page that performed the redirection. Read on incase you encounter window.location.href window is not defined where location replace does not. However, if you use the location.replace function (window location replace aka replace method) it will redirect the user, but not add the current page to the user's history. Thus when the user clicks the Back button they are returned to the page before the page that they were redirected from replace function with the location object. This would be important in the automatic redirect situation to update the session history, as clicking the Back button might keep the user in an endless back button loop. Enjoy! Published on Feb 9, 2018 Tags: Javascript
| JavaScript
| location.href
| location.replace
Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article
that you just finished reading.
No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner
or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in
and improve your skillset with any of the tutorials below.
window.location.href = '';
window.location.replace('');
Understanding this.location.href
How does location.replace differ?
Related Posts
Tutorials
Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.