Preserve window.history.back() to use later SOLVED

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Preserve window.history.back() to use later SOLVED



I'm working on a checkout and i've a back button which triggers window.history.back() on click.


window.history.back()



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.


window.history.back()


window.reload()



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.


window.history.back()


window.history.back()



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.


history.go(-2)



Maybe a global counter set to -1 and then --counter; when entering or reloading cart will do the trick with history.go(counter);


-1


--counter;


history.go(counter);



Any suggestion to deal with this situation?
Thanks.



* EDIT *
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!





That mean you want to prevent browser to go back?
– Som
4 hours ago






maybe you can save the last page url in a cookie and then load that url in the button as href
– Daut
4 hours ago





How do you get to the checkout page? While not ideal, there are plenty of large sites that include a &SourceUrl= when going to a central page (eg checkout / error page / login page). This gives your button a concrete return url without needing to worry about history. It does mean changing your checkout button wherever it appears.
– freedomn-m
4 hours ago


&SourceUrl=





Cookies are a bad idea as you'll need to destroy it, and when do you destroy this cookie or value? User could never press back button and the value will be preserved on this cookie with a sort of problems it may cause.
– Joel Bonet Rodríguez
4 hours ago





yup, setting an uri value could help but prestashop can change it when user interacts and i can't figure out how to ensure it, by the other way, there's a prettylink that kills usability and a link share could be pretty uggly. I'm dealing with a global var but donno why it is destroyed on page reload.
– Joel Bonet Rodríguez
4 hours ago




1 Answer
1



Try


window.location.replace(document.referrer);





The result will be the same as history.back(). If the page reloads, the referrer of this document is the same page.
– Joel Bonet Rodríguez
9 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered