7

Using history.pushState, we can change the current url using history API. Using popstate function, we can get back to previous page. But while going back, I found that the forward link button in browser "Click here to go forward" gets disabled. Now, using history, I need to access that button's property.

How can we access the forward button's url using history API?

Ganesh Babu
  • 3,250
  • 8
  • 31
  • 60

2 Answers2

2

Are you manually popping the state? It sounds like it's getting removed.

To go back, you should be using history.back(). history.back() will preserve the entry in history so that you'll see the forward button enabled to go back forward.

You can also specify how many entries to go back with history.go(X). ex: history.go(-3) will take you back three pages.

Source: MDN

Nathan Tuggy
  • 2,239
  • 27
  • 28
  • 36
Etai
  • 1,363
  • 1
  • 10
  • 13
  • I am using Ajax and just need my url to get change as in history.pushState(). history.back() simply loads the page in the history. – Ganesh Babu May 07 '14 at 07:08
  • You wrote "But while going back, I found that the forward link button in browser "Click here to go forward" gets disabled" If you use history.back(), then the forward will not get disabled... what are you doing at that stage? Can you provide a jsfiddle? Can you give an example of a scenario and how you would expect it to work? – Etai May 08 '14 at 08:13
0

I prefer AJAX Method, when back(or forward) occurred all js variables(page scope) have old-values. firstly you product a JS variable with time(Server) then check this value by AJAX.when Hacker(Super-Beginner) want to access url via back/forward , this method show alert('You are Hacker.Oops') and change location.

page.php

<script type="text/javascript">
<?php $expire=time()+120; // 2 minute
$expire_hash=md5($expire+'SECRET-KEY'); // optional for security purposes
?>
$.get('anti-back-forward.php?<?php echo "expire=$expire&expire_hash=$expire_hash"?>',
function(content){
   if(content=='BAD'){
      alert('You are hacker.Oops');
      window.location='index.php'; 
   }
});
</script>

anti-back-forward.php

extract($_GET);
if(($expire<time()) ||  $expire_hash!=md5($expire+'SECRET-KEY'))
   die('BAD');
echo "OK";
MajidTaheri
  • 3,288
  • 6
  • 24
  • 43