Alex Rivera | Logout

JavaScript KeyCode Values are "undefined" in Internet Explorer 8

Asked 2009-11-17T16:53:45.837
11

I'm having trouble with some JavaScript that I've written, but only with Internet Explorer 8. I have no problem executing this on Internet Explorer 7 or earlier or on Mozilla Firefox 3.5 or earlier. It also executes properly when I use compatibility mode on Internet Explorer 8.

What I'm doing is overriding the Enter keystroke when a user enters a value into a textbox. So on my element I have this:

<asp:TextBox ID="ddPassword" runat="server" TextMode="Password" onkeypress="doSubmit(event)" Width="325"></asp:TextBox>

And then I have the following JavaScript method:

function doSubmit(e)
{
    var keyCode = (window.Event) ? e.which : e.keyCode;
    if (keyCode == 13)
        document.getElementById("ctl00_ContentPlaceHolder1_Login").click();  
}

Again, this all works fine with almost every other browser. Internet Explorer 8 is just giving me a hard time.

Any help you might have is greatly appreciated.

UPDATE: Thanks everyone for your quick feedback. Both Chris Pebble and Bryan Kyle assisted with this solution. I have awarded Bryan the "answer" to help with his reputation. Thanks everyone!

Edit
Report

2 Answers

1

I personally prefer the multi-key approach. This allows multiple keys to be detected, but also a single key just the same, and it works in every browser I've tested.

map={}//declare object to hold data
onkeydown=onkeyup=function(e){
    e=e||event//if e doesn't exist (like in IE), replace it with window.event
    map[e.keyCode]=e.type=='keydown'?true:false
    //Check for keycodes
}

An alternative method would be to separate the onkeydown and onkeyup events and explicitly define the map subitems in each event:

map={}
onkeydown=function(e){
    e=e||event
    map[e.keyCode]=true
}
onkeyup=function(e){
    e=e||event
    map[e.keyCode]=false
}

Either way works fine. Now, to actually detect keystrokes, the method, including bug fixes, is:

//[in onkeydown or onkeyup function, after map[e.keyCode] has been decided...]
if(map[keycode]){
    //do something
    map={}
    return false
}

map[keycode] constitutes a specific keycode, like 13 for Enter, or 17 for CTRL.

The map={} line clears the map object to keep it from "holding" onto keys in cases of unfocusing, while return false prevents, for example, the Bookmarks dialog from popping up when you check for CTRL+D. In some cases, you might want to replace it with e.preventDefault(), but I've found return false to be more efficient in most cases. Just to get a clear perspective, try it with CTRL+D. Ctrl is 17, and D is 68. Notice that without the return false line, the Bookmarks dialog will pop up.

Some examples follow:

if(map[17]&&map[13]){//CTRL+ENTER
    alert('CTRL+ENTER was pressed')
    map={}
    return false
}else if(map[13]){//ENTER
    alert('
answered 2012-10-15T17:36:12.833
0

I think window.Event.keyCode works in IE8 (I can't test right now though)

answered 2009-11-17T16:59:45.987

Your Answer