news

Catching browser back

Today we searched for a solution to catch the browser back button.

The problem is, that the browser per default has no way to catch the back and forward actions. Also the browser does not allow to modify the browser history.

But there are two things you can do:

  1. call location.replace to load a diffrent url and replace the history of the current page.
  2. add a hash (like its used for anchor links) to the url with location.hash='hash'

Basically the idea was to add a hash when the page is loaded. When the user then clicks the browser back button, the url just changes back to the url without the hash. Then all you need to do is to detect that url change and react like you want (e.g. set a new location.href)


Here is the example

 

$(function(){

var lastjavascripthash='';

//if the current url has a hash on dom ready - than the user loads this url directly in browser
if (location.hash) {
alert('you navigated to this link using the browser address!');
lastjavascripthash=location.hash;
}
//just add a dummy hash:
lastjavascripthash=location.hash='#loaded';

//optional - when clicking the links append the hash to the url:
$('#tostep1').click(function() {
lastjavascripthash=location.hash='#step1';
});

//do regular check if the hash in the location still fits the last hash that was set via a valid javascript call
// if not it means someone else modified the hash - typically the browser back
setInterval(function() {
if (location.hash != lastjavascripthash) {
alert('you navigated to this link using the browser back!');
lastjavascripthash=location.hash;
}
}, 300);
}

Since the loaction.hash declaration is not interpreted by all browsers, a better way is to use one of the history javascript plugins.

This is the case in this modified example
(using jquery.history.js Links: plugins.jquery.com/project/history; nix.lv/history/)

var browserback = {
lastjavascripthash: '',

setLastHash: function(hash) {
browserback.lastjavascripthash=hash;
},
historyChangeHandler: function(hash) {
if (browserback.lastjavascripthash != hash) {
alert('you propably used browser back!');
browserback.setLastHash(hash);
}
},

disableBrowserBack: function() {
$.history.init(this.historyChangeHandler);
var hash=new Date().getTime()+ Math.floor(Math.random()*1000)
browserback.setLastHash(hash);
$.history.load(hash);
}
}


$(function(){
browserback.disableBrowserBack();
}

Submitting your vote...
Rating: 5.0 of 5. 1 vote(s). (Click the rating bar to rate this item.)
  1. Oliver http://www.netgenerator.de 03.02.10 10:49

    Vielen Dank für diesen Workaround. Das erhöht die Usability enorm und hat uns auch schon immer interessiert, doch dank euch brauchen wir das ja jetzt glücklicherweise nicht mehr entwickeln. Thanks!

  2. Chris 26.10.09 09:39

    Good point.

  3. Daniel 22.10.09 14:31

    Sure. But detecting the browser back could be the first step to support it.

    That was actually the problem we need to solve here.

  4. Chris 22.10.09 11:02

    Instead of catching the back button one should think of how to support it. It's generally not a really good idea to intervent in the user's software. Think one step further, intercepting the browser's close-button to get more time-per-visit?

  5. Daniel 21.10.09 22:40

    Just found more ideas here:
    http://webdevlopementhelp.blogspot.com/2009/07/disable-back-button-of-browser.html

CAPTCHA image for SPAM prevention 

Login und Registrierung

toggle
...type your search above...
blogroll
Linkhandler für TYPO3 4.2 (linkhandler)
06.08.08 - Diese Erweiterung ist für die Version 4.2 von TYPO3, funktioniert aber durch einen mitgelieferten...
Ajax Framework (fe_ajax)
06.08.08 - Easy way to get barrierfree AJAX features in your extension