Saturday, October 24, 2009

Override backspace as the back button

I have a common problem. I write a lot of web applications that run in a portal. When a user hits the back button it will take them to a different portlet or log them out of the portal completely. I can't prevent them from clicking the back button, but IE and Firefox both have the backspace key set up as a shortcut for the back button. This is annoying because the users will accidentally type it thinking that the cursor is in a text field. When it's not the back button is pressed and the user complains that they were randomly logged off.

To prevent this I use the javascript Shortcut.js and a small chunk of code.

shortcut.add("backspace", function() {
// override backspace as the back button
},{ 'type':'keydown', 'disable_in_input':true, 'propagate':false });

That's all there is to it.

No comments: