Ganesh Babu
Ganesh Babu

Reputation: 3670

How to enable/disable "click to go forward" button in browser using history js?

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?

Upvotes: 7

Views: 3290

Answers (2)

Etai
Etai

Reputation: 1493

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

Upvotes: 2

MajidTaheri
MajidTaheri

Reputation: 3983

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";

Upvotes: 0

Related Questions