Jump to content

Unexpected function behavior.


atar.yosef
 Share

Recommended Posts

Hi there!!I don't know why, but when I am loading the following code snippet in my iPod touch browser, so when I hit the "Backspace" key, the function is called twice instead of being invoked once. Can someone help me know why does this occur?Thanks in advance!!Atar.

<html><head><title></title><script type="text/javascript">try{function myFunction(e){if(e.keyCode==8){alert("The Backspace button has been pressed");}//end if.}//end myFunction().}//end try statement.catch(err){alert("there's an error in this page. \n the error message is:\n" + err.message + "\n the error name is: \n" + err.name + "\n and the error details are:\n" + err.constructor + "\n and the error stack is: \n" + err.stack);}//end catch statement.</script><style type="text/CSS"></style></head><body><input type="text" onkeydown="myFunction(event)" value="something"></input></body></html>

Link to comment
Share on other sites

  • 2 weeks later...

Yes, the event onkeydown gets called repeatedly if you hold a key.I had a similar problem in firefox. But this only happens after a certain delay. You can avoid this by using an onkeyup event:

<html><head><script>var hasPressed=false;function myFunction(e,keyDown) {  if (hasPressed && keyDown) return;   hasPressed=keyDown;   // Code}</script></head><body><input type="text" onkeydown="myFunction(event,true)" onkeyup="myFunction(event,false)"></body></html>

Edited by ReGA
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
 Share

×
×
  • Create New...