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!
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.
An alternative method would be to separate the
onkeydown
andonkeyup
events and explicitly define the map subitems in each event:Either way works fine. Now, to actually detect keystrokes, the method, including bug fixes, is:
map[keycode]
constitutes a specific keycode, like13
forEnter
, or17
forCTRL
.The
map={}
line clears the map object to keep it from "holding" onto keys in cases of unfocusing, whilereturn false
prevents, for example, the Bookmarks dialog from popping up when you check forCTRL+D
. In some cases, you might want to replace it withe.preventDefault()
, but I've foundreturn false
to be more efficient in most cases. Just to get a clear perspective, try it withCTRL+D
.Ctrl
is17
, andD
is68
. Notice that without thereturn false
line, the Bookmarks dialog will pop up.Some examples follow:
One thing to keep in mind is that smaller combinations should come last. Always put larger combinations first in the if..else chain, so you don't get an alert for both
Enter
andCTRL+ENTER
at the same time.Now, a full example to "put it all together". Say you want to alert a message that contains instructions for logging in when the user presses
SHIFT+?
and log in when the user pressesENTER
. This example is also cross-browser compatible, meaning it works in IE, too:Note that some special keys have different codes for different engines. But as I've tested, this works in every browser I currently have on my computer, including Maxthon 3, Google Chrome, Internet Explorer (9 and 8), and Firefox.
I hope this was helpful.
I think
window.Event.keyCode
works in IE8 (I can't test right now though)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.Or something like that. var keyCode = e.which || e.keyCode;
Just a hunch, try this:
try this: