Change document.location.hash in history

On the product list page, by clicking the buy link, I add the article to the cart using Ajax and put it in document.location.hash. When I delete an article from the recycle bin and return using the back button in the browser, I need to remove the product identifier from location.hash on the product list page. Is it possible?

+4
source share
1 answer

Nope. You can’t change the history in browsers by design, imagine what the security implications will be? For example, you can insert something into history and give history.back () to send the user to where you want!

You must handle the back-button elements on the server side of the sessions using some state controller.

Add: when the user clicks the back button, the page is retrieved from cahce or from the server, depending on the header information, etc. The browser has already rendered the page whenever your code starts working. Then, changing the location object will result in additional loading / reloading of the page. If you KNOW that the identifier is invalid, there is no need to remove it from the location hash, you can process it within the server code.

+2
source

Source: https://habr.com/ru/post/1486471/


All Articles