Jump to content

I'm Getting Pissed :<


Yuval200

Recommended Posts

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 defined
Now, 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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...