Home > Article > Web Front-end > Comparison of href and replace in javascript (detailed explanation)
When using JavaScript, sometimes we are too familiar with frequently used methods and ignore the subtle differences in principles between them.
Examples are as follows:
window.location.href, window.location.replace.
Both of these methods can make the page jump to a new page, but I ignored the details after the jump, such as the original page returned.
The href in window.location.href is actually the href in the tag. After using this to jump to the page, you can use the browser's back button to return to the original page, or you can use history The .go(-1) function jumps to the original page.
window.location.replace(url) method allows the browser to clear the URL in its history and reset the URL. When using the browser's back button or forward button, it will not be able to jump to the original page, but will jump to the previous page of the original page.
That is, the replace() method will not generate a new record in the History object. When using this method, the new URL will overwrite the current record in the History object.
The above comparison of href and replace in javascript (detailed explanation) is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.
For more related articles on the comparison of href and replace in javascript (detailed explanation), please pay attention to the PHP Chinese website!