0

I am using VS 2010.

I use two links to go back/forward between pages.

Code:

<a href="#" onclick="history.go(-1);return false"><img src="Images/back.jpg" /></a>
<a href="#" onclick="history.go(1);return false"><img src="Images/forward1.jpg"/></a>

It works well. Now I want to enable and disable the links based on browser history.

How do I do this using JavaScript?

Tim M.
  • 53,671
  • 14
  • 120
  • 163
Pooja
  • 495
  • 3
  • 9
  • 25
  • possible duplicate of [Disabling Back button on the browser](http://stackoverflow.com/questions/87422/disabling-back-button-on-the-browser) – KV Prajapati Dec 30 '11 at 04:57
  • I'm sure people don't want you playing around with their browser history, especially behind the scenes. – Purag Dec 30 '11 at 08:28

1 Answers1

0

As anchors are used for clicking, any other dom element also can be clicked. So,It is visually better to implement this functionality without using hyperlinks. Use code similar as below:

<p id="back"><img src="back.jpg"></p> 
<p id="forward"><img src="forward.jpg"></p>

and event as below:

$("#back").click(function(){
  if(!$(this).hasClass('disabled'))   history.go(-1);
  $(this).addClass('disabled');
  $("#forward").removeClass('disabled');
});

$("#forward").click(function(){
   if(!$(this).hasClass('disabled')) history.go(1);
  $(this).addClass('disabled');
  $("#back").removeClass('disabled');
});

User required style for .disabled.

Umesh Patil
  • 10,475
  • 16
  • 52
  • 80