0

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. Lets us say that the main page the user is on is 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

<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.

4DAWIN
  • 21
  • 7
  • The back button only works on what is recorded in the web browser history. If a website could manipulate that, it would create serious security concerns. Image the hijacking that could be done. – Difster Jul 31 '17 at 06:20

2 Answers2

1

I would not use HTML5 history.pushState for this as it is not supported for IE9 and previous versions. But I can give you a better solution. You can use sessionStorage and performance.navigation.type.


sessionStorage is a storage type like localStorage but it only saves your data for the current tab.

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.

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
}
Ahmet Can Güven
  • 5,392
  • 4
  • 38
  • 59
0
   var c_link = "www.example.com/time&val=1&base=2&timeid=3&rfid=4"; 

Please try to pas the value through the url itself.

Pranav MS
  • 2,235
  • 2
  • 23
  • 50
  • Those paraments may change they are not static. so every time time the usder visits the page they will be different – 4DAWIN Jul 31 '17 at 06:22
  • I want to take the parameters from from the current url and append them to redirected url – 4DAWIN Jul 31 '17 at 06:24
  • the script itself you can add the dynamic values . And the pass it on .what is your server side language ? – Pranav MS Jul 31 '17 at 06:27