I'm working on a checkout and i've a back button which triggers window.history.back()
on click.
Works fine till the user make some change in cart, then the page reload and window.history.back()
do the same than window.reload()
because it gets the last page.
I know the best workaround with this could be apply Ajax to this cart updates by user input to keep window.history.back()
inmutable to prev page before entering cart. But this is not possible on this project, it's a temporary prestashop and it will be fine to preserve window.history.back()
when click on some modification button before submit to mantain the functionallity on back button.
I think this is not possible due to privacity but i want to know if someone had this problem before and which workaround will be better.
Using history.go(-2)
will send to user two steps back so it's not ok for some use cases.
Maybe a global counter set to -1
and then --counter;
when entering or reloading cart will do the trick with history.go(counter);
Any suggestion to deal with this situation? Thanks.
Solved using the following workaround:
Code:
/* inside the page where we want to preserve the "back" URI */
<script>
var counter = 0;
if(window.name != ""){
/* if window.name is set, assign the value to var counter*/
counter = window.name;
} else {
/* if it's not, init to 0. */
counter = 0;
}
/* Set window.name to counter value minus 1. It will be set to -1 the first time you enter the cartPage (on this example case) and it will be changed to -2, -3 etc each time you reload. */
window.name = counter-1;
</script>
/* On global.js */
if(window.location.href.indexOf("cartPage") === -1) {
/* Reset window.name value if we're not on cartPage, to avoid errors */
window.name = "";
}
/* The button: */
<a onclick="history.go(window.name)"> go back </a>
Hope it helps someone. Cheers!