Forum Moderators: open

Message Too Old, No Replies

Back Link Show and Hide?

         

greencode

3:10 pm on May 3, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



I'd like to use the
a href="javascript: history.go(-1)
as a link on a Back Button I have on a number of pages but the only trouble with this is if you go direct to that page and therefore there's no page to go back to then the button doesn't do anything.

Is there any javascript out there that will show the link if there's a page to go back to or hide it if there's not?

Fotiman

4:05 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The history object has a length property. You could check to see if history.length > 1.

greencode

4:13 pm on May 3, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks for that - any idea how to implement? I've no idea!

Fotiman

5:15 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For example, put this just before the closing </body>:

<script type="text/javascript">
// Note, I'm defining window.onload but a better
// approach would be to attach an event listener to the
// window load event (for example, using your favorite
// framework like jQuery, yui, etc., or defining your
// own method for attaching event listeners)
window.onload = function () {
var backBtn;
// See if this window has history
if (window.history.length > 1) {
// It does, so lets create the back button
backBtn = document.createElement('input');
backBtn.type = 'button';
backBtn.value = 'Back';
backBtn.onclick = function () {
window.history.go(-1);
};
// Attach this element wherever you want it
document.body.appendChild(backBtn);
}
}
</script>

greencode

5:31 pm on May 3, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Many thanks for this Fotiman - I'll implement and report back.