faqts : Computers : Programming : Languages : JavaScript : Event handling

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

34 of 70 people (49%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

How do I get a event when the user presses the Back/Forward key from javascript ?

Apr 30th, 2001 04:32
Colin Fraser, anupriya ramraj,


I am assuming you mean the Back and Forward buttons on the browser 
toolbar here and I dont think you can, which has more to do with the 
connectionless nature of the Web than Javascript. However, you can force 
events by replacing the current history object location with one you 
have created.  This will replace the current back URL, well it should 
anyhow. 
 <script language="JavaScript"><!--
 if (document.images)
     location.replace('http://www.myserver.com/myfolder/otherpage.htm');
 else
     location.href = 'otherpage.htm';
 //--></script>
You need to replace the whole string in the history object or it may not 
work properly. Any browser that does not support the images object will 
accept the link to your page and it is just a link. To do a similar 
thing forward, that will only work if the browser window actually has a 
page to go forward to. You can do this by opening a new document then 
sending the browser back to where it came from, but this is clumsy and 
will, no doubt, just annoy too many users.    
If you are talking about a back or forward button on the page itself, 
then a function is simple to build from the onClick event. 
<a href="javascript:void" onClick="doThis()><img src="back.gif"></a>
should be sufficient.