开发者

javascript - stopping global functionality

开发者 https://www.devze.com 2023-03-27 08:00 出处:网络
I\'m working on a legacy application and I found this javascript snippet: function keypress(e) { var evt = (e) ? e : window.event;

I'm working on a legacy application and I found this javascript snippet:

function keypress(e) {
    var evt = (e) ? e : window.event; 
    switch (evt.keyCode) {
        case 9:case 13: {
        ...
        }

This has been working great, however I have added new functionality to the application using a specific function that is called from the o开发者_JAVA技巧nKeyPress of a <div>, lets call it function xyz(). This function works great except when the enter key is pressed. In my new function I can capture the keycode 13 (the enter key), but I can not stop it from going to this function above. When the Enter key is pressed in my new functionality the code above causes it to error.

I need a way to prevent the Enter key from going to the keypress(e) function (above). Is there a way I can stop it from doing this.

function xyz(event)
{
   if(event.keyCode == 13){
      //  I need to stop the process of the Enter key here
      ...
   }
}


This solution should be entirely unobtrusive, just tested something similar:

new function() {
    var old_kp = window.keypress;
    delete window.keypress;
    window.keypress = function(e) {
        switch (evt.keyCode) {
            case 13: /* do your thing */
            default: {
                return old_kp(e);
            }
        }
    }
}();

You can call the function the same way as before, it wraps the old function and in cases other than '13' you just delegate to the old implementation.


Edit:

To cancel the propagation of the 'Enter' key event, have a look at the section Turning it off. It will tell you how you can prevent propagation in a cross-browser-compatible way:

function xyz(e) {
    if(event.keyCode == 13) {
       if (!e) var e = window.event;
       e.cancelBubble = true;
       if (e.stopPropagation) e.stopPropagation();
    }
}

Old versions of IE do not pass an event to the event handler, you need to retrieve window.event instead. That's why we first check whether e has been assigned - if not, we assign window.event to e. The cancelBubble property is also IE-specific, setting it will do the trick for IE and other browsers won't complain, they simply overlook the property. The final check is whether the method stopPropagation exists for the event. stopPropagation is the solution recommended by the W3C event model. All modern browsers support this (including IE 9). Using this code you will cover all existing possibilities for stopping event propagation in all more or less modern browsers.


I suppose you should something like:

function keypress(e) {
    var evt = (e) ? e : window.event; 
    switch (evt.keyCode) {
        case 9: {
        ...}
        break;
        case 13: ...//your code
        break;
        }


Just make the event listener listen to your function first, and then call keypress?


I may be misunderstanding what you are asking, you should be able to just separate the "Enter" case in the switch statement, as shown:

function keypress(e) {
    var evt = (e) ? e : window.event; 
    switch (evt.keyCode) {
        case 9:
            //Tab Key Pressed
             break;

        case 13: 
            //Enter Key Pressed
            break;
     }
}

In order to stop the execution in keypress, you could check what key was pressed prior to the switch statement, or just return false inside the case 13:

function keypress(e) {
        var evt = (e) ? e : window.event; 
        if(evt.keyCode != 13)
        {
            switch (evt.keyCode) {
                case 9:
                    //Tab Key Pressed
                    break;

                case 13: 
                    //Enter Key Pressed
                    break;
            }
        }
   }

or

function keypress(e) {
        var evt = (e) ? e : window.event; 
        switch (evt.keyCode) {
            case 9:
                //Tab Key Pressed
                 break;

            case 13: 
                return false;
                break;
         }
    }
0

精彩评论

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