Java Tip 15: How to make a "Back" button in JavaScript
How to make a "Back" button in JavaScript
By John D. Mitchell, JavaWorld.com, 08/01/96
Do you have a hankering for a button that you can put anywhere you please in your HTML document -- and that allows users to
go back to the document they were previously viewing? Perhaps this is a sign of my laziness, but I think about it regularly.
I hate to have to move the mouse cursor all the way up to the browser's "Back" button when scrolling through long pages.
Well, I don't have a Java solution for you but Robert Raught sent in this cute little JavaScript code that provides the functionality of the browser's "Back" button:
<a href="javascript:history.go(-1)" onMouseOver="self.status=document.referrer;return true">Go Back</a>
Check out this example of the BackButton script in action.
Some things to note:
- It only works with browsers supporting JavaScript (i.e., Netscape Navigator).
- It shows the target URL in the status bar but:
- For some bizarre reason it does so only when the page was referred to via a hotlink.
- It does not show the target URL if you type the URL directly or if you use Java's showDocument() method to display a page.
Enjoy!
About the author
Subsisting on caffeine, sugar, and too little sleep, John D. Mitchell has been consulting for most
of the last nine years, and developed PDA software in OO assembly
language at Geoworks. He funds his Java addiction by writing
compilers, Tcl/Tk, C++, and Java systems. He co-authored the hot
new Java book Making Sense of Java and is
currently developing a Java compiler.