How can I prevent Backspace from taking me back to the previous page while I use the login box

the login box is in the header of the website, availalbe on all pages. I'm using a javascript :

<script type="text/javascript" language="javascript">
       history.forward();

Which solves the issue of the backspace in the login box. But while I'm browsing the rest of the website, I can use backspace to see the previous page, I'd have to use the navigation menu only. How can I solve that issue ?

Answers


Try this JavaScript

<script for="document" event=onkeydown>

// Check if this is a Backspace
 if (window.event.keyCode == 8)
 {
    // Cancel backspace if not in a text element
    if(document.activeElement.type != "text")
    {
        window.event.returnValue=false;
    }
 }
 </script>

A webpage changing the key bindings of the browser is a controversial topic, as this violates the principle of the least surprise (which should be familiar to an UX designer). Also, not all browsers have this key mapping (Bksp -> Go back).

That said: you can try capturing the key events, as described e.g. here and cancelling the event propagation. Not sure if this is possible for all keys, so you'd have to try and see.


If you're using a proper HTML text input box and the focus is on it then backspace will not take the user back. If you're not using a proper HTML input box then I suggest you do so. Browsers handle this stuff for you.


Need Your Help

How to convert this observe_field to jquery

javascript jquery ruby-on-rails prototypejs

I would like to use jquery instead of prototype and I am a bit lost to convert this observe_field in jquery.