简体   繁体   English

将参数添加到后退按钮重定向到上一个网址

[英]Add paraments to back button redirect to previous url

Ok so I have this code that allows me to redirect the users on my current page to any other page when they click the back button 好的,所以我有了这段代码,当他们单击后退按钮时,我可以将当​​前页面上的用户重定向到其他任何页面

The code works fine but now I would like to pass the URLs parameters from the previous page to the redirected page so for example. 该代码可以正常工作,但是现在我想将URL参数从上一页传递到重定向的页面,例如。 Lets us say that the main page the user is on is www.example.com/ready&val=1&base=2&timeid=3&rfid=4 可以说用户所在的主页是www.example.com/ready&val=1&base=2&timeid=3&rfid=4

I would like the &val=1&base=2&timeid=3&rfid=4 passed on to the end of the domain that the user is being redirected to when they click the back button 我希望&val = 1&base = 2&timeid = 3&rfid = 4传递到用户单击后退按钮时将重定向到的域的末尾

<script type="text/javascript">
  !function(){
    var c_link = "www.example.com/time"; 
    var t;             
    try{
      for(t=0;10>t;++t)history.pushState({},"","#");
      onpopstate=function(t){t.state&&location.replace(c_link)}}
    catch(o){}
  }();
</script>

So the user gets to www.example.com/time I would like the &val=1&base=2&timeid=3&rfid=4 to be and the end of it. 因此,用户进入www.example.com/time,我希望&val=1&base=2&timeid=3&rfid=4成为其结尾。

I would not use HTML5 history.pushState for this as it is not supported for IE9 and previous versions. 我不会为此使用HTML5 history.pushState ,因为IE9和早期版本不支持它。 But I can give you a better solution. 但我可以为您提供更好的解决方案。 You can use sessionStorage and performance.navigation.type . 您可以使用sessionStorageperformance.navigation.type


sessionStorage is a storage type like localStorage but it only saves your data for the current tab. sessionStorage是一种类似于localStorage的存储类型,但它仅保存当前选项卡的数据。

Session storage can be used like this. 会话存储可以像这样使用。

sessionStorage.setItem('key', 'value'); //saves the value
sessionStorage.getItem('key'); //gets the saved value

performance.navigation.type is the browser is the variable that hold users navigation info. performance.navigation.type是浏览器,是保存用户导航信息的变量。

if(performance.navigation.type == 2){
  //User is coming with back button
}

With the information above we can solve your problem easily. 通过以上信息,我们可以轻松解决您的问题。

Script on next page 下一页脚本

var values = {
    val: 1,
    base: 2,
    timeid: 3,
    rfid: 4
}
sessionStorage.setItem('returnData', JSON.stringify(values))

Here is our main page. 这是我们的主页。

if(performance.navigation.type == 2){
  var backValues = JSON.parse(sessionStorage.getItem('returnData'));
  //Do whatever you need with backValues
}
   var c_link = "www.example.com/time&val=1&base=2&timeid=3&rfid=4"; 

请尝试通过网址本身输入该值。

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

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