Jose3d
Jose3d

Reputation: 9277

back button browser causes go to home page

I would like to understand more about how to capture if a user click on the back button of the browser, and what are the basic tecniques to handle it.

As an example of this do you know how to redirect (for instance) to the user to the home page if the back button is pressed?

Upvotes: 0

Views: 738

Answers (2)

pconcepcion
pconcepcion

Reputation: 5641

You can add a function to document onKeyPress:

document.onkeypress = myKeyPressHandler;
// NOTICE: IS NOT a good idea to change the behabiour of backspace  
function myKeyPressHandler() {
    if (window.event && window.event.keyCode == 8) {
         // Handle backspace
         // I. E. Go to the home page
         window.home(); // 
    }
}

Actually changing the default beahabiour of backspace is not a good practice and you should avoid doing it unless you have very good reason or you will confuse your users.

Upvotes: 0

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114377

You can't and you shouldn't. Users expect their back button to work in a certain way, changing this breaks this behaviour. Bad, bad.

Upvotes: 5

Related Questions