user1927944
user1927944

Reputation:

change document.location.hash in history

On product list page in pressing the reference "buy" , I add the article to cart with a help of Ajax and put down Article Id to document.location.hash. When I delete the article from the cart and return with a help of the button "back" in the browser , I need to delete product Id from location.hash on product list page. Is it possible?

Upvotes: 4

Views: 138

Answers (1)

Axel Amthor
Axel Amthor

Reputation: 11096

Nope. You can't modify history in browsers by design, imagine what security impacts that would have? You could, for instance, push something in to the history and issue a history.back() to send the user wherever you want!!

You must handle the back-button stuff in the session server side with some state controller.

Add: when user hits 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 will start running. Modifying the location object then would result in an additional page load/reload. If you KNOW that the ID is invalid, there's no need to delete it from the location hash, you might handle that within the server code.

Upvotes: 2

Related Questions