homepage Welcome to WebmasterWorld Guest from 54.166.173.147
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Action Listeners in javascript - IE
javascript action listeners - IE compatible
nil111




msg:4195946
 7:03 am on Sep 3, 2010 (gmt 0)

I'm trying to attach an event to detect key press in internet explorer.

the current works for firefox but not for internet explorer.

<script type="text/javascript">
//window.addEventListener("keyup",function(evt){myfunc(evt);},false);

function myfunc(e)
{
if(e.keyCode==17){
alert('ctrl pressed');
}
else {alert("ok");}
}



//set page event handlers
if (window.attachEvent) {
//IE and Opera
window.attachEvent("keyup", function(evt){myfunc(evt);},false);
} else if (window.addEventListener) {
// IE 6
window.addEventListener("keyup", function(evt){myfunc(evt);},false);
} else {
//FireFox
document.addEventListener("keyup", function(evt){myfunc(evt);},false);
}


</script>



Thanks in advance!

 

nil111




msg:4195955
 7:36 am on Sep 3, 2010 (gmt 0)

oh btw keyCode 17 is for the ctrl key

Fotiman




msg:4196041
 1:38 pm on Sep 3, 2010 (gmt 0)

attachEvent [msdn.microsoft.com] takes only 2 parameters (not 3 as you have it), the event must be specified as "onkeyup" instead of "keyup", and it needs to be attached to the document object instead of the window object.


document.attachEvent("onkeyup", function(evt){myfunc(evt);});


Also, your else if and else conditions are the same, and you incorrectly marked the 'else if' condition with a comment indicating that it's for IE6 (which is not the case... IE6 will use the attachEvent method.

Hope that helps.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved