Yuval200 Posted December 1, 2006 Share Posted December 1, 2006 Hello AGAIN guys :)Anyway, I was surfing the HTML DOM tutorial, and found some cool things with the event object like this and this, I fought that if it was possible to alert the key that was pressed, I can also assign an action to it if it is clicked. I made a simple function in the head section, and then made the function to work by adding the <body> tag a "onkeyup" attribute that contains the function.. Now, that works fine, but I didn't wanted that, so I made something else in the body section. <script type="text/javascript"><!-- function keylogoff(event) { if (event.keyCode == 65) alert('hello'); } document.body.onkeyup = keylogoff(event);//--></script> As you guessed, it didn't worked..I made it simple as I can so I can debug it, but nothing. In FF WTF, the Javascript Inspector wrote: event is not definedNow, it might think that "event" is a varible, but it isn't. I wrote "event" in the phar-something (those "()") because the "Tryit" example did it too.Can you help me?Thanks Link to comment Share on other sites More sharing options...
aspnetguy Posted December 1, 2006 Share Posted December 1, 2006 when assigning a function to an event you cannot pass variables directly. document.body.onkeyup = keylogoff;//if not passing variables document.body.onkeyup = function(){keylogoff(event)};//if you are passing variables Link to comment Share on other sites More sharing options...
Yuval200 Posted December 1, 2006 Author Share Posted December 1, 2006 Thanks again aspneguy, but it didn't worked :<I guess I'll have to move the function to the head section untill I'll find a solution. Link to comment Share on other sites More sharing options...
jesh Posted December 4, 2006 Share Posted December 4, 2006 You might try this as your function instead: function keylogoff(e){ var evt = (e) ? e : event; var charcode = (evt.which) ? evt.which : evt.keyCode; if(charcode == 65) { alert('hello'); }} And try document.onkeyup = keylogoff; rather than document.body.onkeyup = keylogoff;This method works for me in both IE and Firefox. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now