繁体   English   中英

保留window.history.back()以便以后使用

[英]Preserve window.history.back() to use later

我正在进行结帐,并且有一个后退按钮,单击后会触发window.history.back()

在用户对购物车进行一些更改之前,它可以正常工作,然后页面重新加载和window.history.back()window.reload()相同,因为它获取了最后一页。

我知道最好的解决方法是通过用户输入将Ajax应用于此购物车更新,以保持window.history.back()在进入购物车之前不可变到上一页。 但这在此项目上是不可能的,这是一个临时的prestashop,可以在单击某些修改按钮之前提交window来保留window.history.back() ,以保持后退按钮的功能。

我认为由于私密性,这是不可能的,但我想知道是否有人之前曾遇到过此问题,哪种解决方法会更好。

使用history.go(-2)会向用户退回两步,因此在某些用例中不可行。

也许全局计数器设置为-1 ,然后设置--counter; 当进入或重新加载购物车时,将使用history.go(counter);

有什么建议可以解决这种情况吗? 谢谢。

使用以下解决方法解决

码:

/* 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>

希望它可以帮助某人。 干杯!

尝试

window.location.replace(document.referrer);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM