The approximate difference between location.href and location.replace

Today, the school's external teacher told us that during the company's development process, some misoperations caused by the novice have caused many problems.

For example, in the project, for example, you want to buy a product, and there is a coupon for this product, and to use this coupon, you need to request a third-party address,

There will be a jump in the middle, because after using location.href, it is no problem to operate according to the process, but if the user clicks back, he cannot jump back to the original page for submitting the order, and the request will be repeated all the time, causing program errors.

Therefore, it must be replaced with location.replace to jump to ensure that it will not be stored in window.history after entering the third party. Users can bypass the third-party address and return to the original page directly by clicking return.

It is also mentioned that when the user clicks back on the ios device, in order to improve performance, the webpage is presented in the form of a snapshot, and the content will not be updated. Therefore, when returning to the webpage, localtion.reload() needs to be used to refresh the page and re-request page content

<body onpageshow="location.reload()">

Usage difference

location.href="http://www.baidu.com"

location.replace("http://www.baidu.com")


location.href will be written to the browser's history window.history object location.replace will not


practice


Here I wrote two buttons, both of which jump to the Baidu website


Click on href first

You can click when you find the browser's return button after the jump



Then click replace

After the jump, the browser's return key cannot be clicked, because replace actually replaces the current url, not a jump, and will not save the record



About this difference, first record

slip away

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326072711&siteId=291194637