Forum Moderators: open
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.
<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>