views:

2132

answers:

5

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. Thanks!

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!

A: 

Try adding onkeyup event as well and call the same function.

TIP: You can add debugger; at beginning of doSubmit to set a break, then you can examine keyCode.

dcp
Interesting, the "onkeyup" event doesn't even execute in this case on Internet Explorer 8.
mbmccormick
A: 

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

Soufiane Hassou
+2  A: 

It looks like under IE8 the keyCode property of window.Event is undefined but that same property of window.event (note the lowercase e) has the value. You might try using window.event.

function doSubmit(e)
{
   var keyCode = (window.event) ? e.which : e.keyCode;
   if (keyCode == 13)
      document.getElementById("ctl00_ContentPlaceHolder1_Login").click();  
}
Bryan Kyle
Excellent! With this code and with Chris Pebble's code from above, this worked perfectly! Thanks!
mbmccormick
+2  A: 

Just a hunch, try this:

var keyCode = e.keyCode ? e.keyCode : e.which;
Chris Pebble
This worked on Internet Explorer 8, but did not work on any of the other browsers that this code used to work on.
mbmccormick
Chris, please see my commenting below. You assitance along with Bryan's helped solve this! Thanks again!
mbmccormick
A: 

Or something like that. var keyCode = e.which | e.keyCode;

Alexsandro