jquery - Are Javascript char codes compatible with all or certain browsers? - Stack Overflow

I have found the following code on the internet, when I saw this solution I wondered if this key code i

I have found the following code on the internet, when I saw this solution I wondered if this key code is the same for all browsers.

var CalendarFilter = Backbone.View.extend({
      // ...
      events: {
        'click .filter':  'filter',
        'keypress input[type=text]': 'filterOnEnter'
      },
      filterOnEnter: function(e) {
        if (e.keyCode != 13) return;
        this.filter(e);
      },
      filter: function(e) { /* ... */ });
      }
    });

Just a doubt, thanks.

I have found the following code on the internet, when I saw this solution I wondered if this key code is the same for all browsers.

var CalendarFilter = Backbone.View.extend({
      // ...
      events: {
        'click .filter':  'filter',
        'keypress input[type=text]': 'filterOnEnter'
      },
      filterOnEnter: function(e) {
        if (e.keyCode != 13) return;
        this.filter(e);
      },
      filter: function(e) { /* ... */ });
      }
    });

Just a doubt, thanks.

Share Improve this question asked Dec 25, 2012 at 2:07 UuidUuid 2,5565 gold badges30 silver badges38 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

First of all, charCode is not keyCode! charCode follows the ascii set, while keyCode is a particular index of the key.. The different values between the two can be seen here: Keyboard Event Character Values for the Lower ASCII Character Set - O'Reilly Answers

One major difference between charCode and keyCode is that charCode is deprecated and typically has no value in some browsers [apart from 0] when referenced

Funnily enough, onkeypress seems to return the character code instead of the keyCode, while onkeyup and onkeydown works as expected, so there may be some issues when detecting keyCode values. You can test this out here JavaScript - Detecting keystrokes - Additional reference: keyCode and charCode.

keyCode, charCode and which is not remended by w3c, however, there is still legacy support for the keyCode model. Solid cross browser/platform support is done with fixed virtual keyCodes that stay independent of keyboard layout - hence being "virtual".

Other Virtual keyCodes - outside of the fixed virtual keyCodes - seems to be consistently implemented across vendors as well: KeyboardEvent - Document Object Model (DOM) | MDN Virtual-Key Codes (Windows)

jQuery uses it's own keyCode/charCode event object property: .which, which attempts to unify keyCode and charCode. And favors keyCode values - event.which – jQuery API

In short, your specific keyCode: "13", should work with most browsers that support javascript as it is a fixed virtual keycode and is consistent with all browsers and platforms

The key codes are standard, but it might be best to check for key code 10 as well. 10 is the key code for "line feed", while 13 is the key code for "carriage return". There's a historical difference between the two (type writers needed to send both signals, a "line feed" to move the paper up and a "carriage return" to move the part that types over to the left side of the paper).

For instance, a quick check shows Ctrl + Enter sends keyCode 10 on Chrome on Windows. It's possible some 'nix systems use keyCode 10, though I can't confirm. And a Google search reveals iPhone may send 10. There are likely other cases.

Some systems use one and some use the other to represent line breaks (last I checked, Windows uses a bination of both), but in the modern world they practically both mean Enter, so it wouldn't hurt to cover both bases.

Of course. It's the same across the entire puter.

The keyCode 13 (newline) has no issue with cross OS or cross Browser patibility. No worries.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742328911a4423311.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信