开发者

How can I disabling backspace key press on all browsers?

I'm trying to disable the backspace button on an order page in all cases except when a textarea or text input is an开发者_StackOverflow社区 active element to prevent users from accidentally backing out of an order. I have it working fine in most browsers, but in IE (testing in IE9, both regular and compatibility mode) it still allows the user to hit the backspace and go to the previous page.

Here's the code:

$(document).keypress(function(e){
        var activeNodeName=document.activeElement.nodeName;
        var activeElType=document.activeElement.type;
        if (e.keyCode==8 && activeNodeName != 'INPUT' && activeNodeName != 'TEXTAREA'){
            return false;
        } else {
            if (e.keyCode==8 && activeNodeName=='INPUT' && activeElType != 'TEXT' && activeElType != 'text'){
                return false;
            }
        }
    });

Any advice on what I'm doing wrong here?

Thanks!


I think you're overcomplicating that. Rather than checking for an active element, find the event target instead. This should give you the information you need. It's also better to use keydown rather than keypress when there is no visible character. Finally, it's better to use e.preventDefault() for better granularity.

$(document).keydown(function(e) {
    var nodeName = e.target.nodeName.toLowerCase();

    if (e.which === 8) {
        if ((nodeName === 'input' && e.target.type === 'text') ||
            nodeName === 'textarea') {
            // do nothing
        } else {
            e.preventDefault();
        }
    }
});

NB I could have done this the other way round, rather than an empty if block and all the code going in the else block, but I think this is more readable.


Instead of keypress, try the keydown function, it will fire before the actual browser based hook. Also, putting in a preventDefault() function will assist in this. IE :

$(document).keydown(function(e){
   e.preventDefault();
   alert(e.keyCode);
});

Hope this helps.


The most Simple thing you can do is add the following one line in the very first script of you page at very first line

window.history.forward(1);


Most examples seem to be for the JQuery framework - Here an example for ExtJS

(I've been getting a lot of downvotes for this recently as the question now has JQuery tag on it, which it didn't previously. I can remove the answer if you like as isn't for JQuery but it's proven to help others not using that framework).

To use this add this code block to your code base, I recommend adding it inside the applications init function().

    /**
     * This disables the backspace key in all browsers by listening for it on the keydown press and completely
     * preventing any actions if it is not which the event fired from is one of the extjs nodes that it should affect
     */
    Ext.EventManager.on(window, 'keydown', function(e, t) {     
       var nodeName = e.target.nodeName.toLowerCase();
        if (e.getKey() == e.BACKSPACE) {
            if ((nodeName === 'input' && e.target.type === 'text') ||
                nodeName === 'textarea') {
                // do nothing
            } else {
                e.preventDefault();
            }
        }
    });


Use e.which instead of e.keyCode; jQuery normalizes this value across browsers.

http://api.jquery.com/keydown/

To determine which key was pressed, examine the event object that is passed to the handler function. While browsers use differing properties to store this information, jQuery normalizes the .which property so you can reliably use it to retrieve the key code.

Then, use e.preventDefault(); to prevent the default behaviour of moving to the previous page.


    <html>


<head>
<script type="text/javascript">


function stopKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 8) && (node.type!="text"))  {return false;}
}

document.onkeypress = stopKey;


    </script>

</head>
<body onkeydown="return stopKey()">
<form>
    <input type="TEXTAREA" name="var1" >
    <input type="TEXT" name="var2" >

</form>
</body>
</html

I had to add the onDownKey attribute to the body in order to get editing keys to go to the functions.


$(document).keydown(function(e) {
    var elid = $(document.activeElement).is('input');
    if (e.keyCode === 8 && !elid) {
        return false;
    }
});

Hope this might help you


Seems like the "backspace" will also act as "navigation back" if you have selected radio buttons, check-boxes and body of document as well. Really annoying for forms - especially when using post. All the form could be lost with one slip of the "backspace" key -_- ...

Honestly... who's idea was it to allow the "backspace as a navigational "back" button!!! really bad idea in my opinion.

I disable the "backspace" default on anything that is not a text area or text field - like this:

 $(document).keydown(function(e){

    console.log(e.keyCode+"\n");

    var typeName = e.target.type;//typeName should end up being things like 'text', 'textarea', 'radio', 'undefined' etc.
    console.log(typeName+"\n");



    // Prevent Backspace as navigation backbutton
    if(e.keyCode == 8 && typeName != "text" && typeName != "textarea"){
        console.log("Prevent Backbutton as Navigation Back"+typeName+"\n");
        e.preventDefault();
    }
    //

})

Not sure where else one would want the normal behavior of a back-button other than in these two areas.


document.onkeydown = KeyPress;
function KeyPress(e) {
  if (!e.metaKey){
     e.preventDefault();
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜