Page 1 of 1

Javascript: Disable Keys

Posted: Fri Jun 16, 2006 9:31 am
by tecktalkcm0391
I now what the key codes are but I can't get it to work I have:

Code: Select all

<body onLoad="javascript:window.resizeTo(275,225);self.resizeTo(275,225);"  onkeydown="if ((event.keyCode==8) || (event.keyCode==122) || (event.keyCode==116)) {event.keyCode=0; return event.keyCode; event.returnValue = false; }">

Posted: Fri Jun 16, 2006 10:57 am
by pickle
What is 'it' that you want to work?

Posted: Fri Jun 16, 2006 12:44 pm
by tecktalkcm0391
I don't want users to be able to click F5 (Refresh) F11 (Full Screen in IE) and Backspace (Back) on a page on my website.

Posted: Fri Jun 16, 2006 12:48 pm
by pickle
Then do your if condition, and if any of those keys are pressed, return false.

Posted: Fri Jun 16, 2006 12:55 pm
by tecktalkcm0391
I did it and it doesn't work!

Posted: Fri Jun 16, 2006 12:57 pm
by pickle
Not sure then - Google probably knows

Posted: Fri Jun 16, 2006 3:19 pm
by Benjamin
It might not be possible, the browser might act on those keypresses before they ever get to the Javascript keydown event.

Posted: Fri Jun 16, 2006 3:23 pm
by pickle
agtlewis wrote:It might not be possible, the browser might act on those keypresses before they ever get to the Javascript keydown event.
Good point. There are two events: onkeypress() and onkeydown() - I'm not sure which one is fired before the key press is handled.

Posted: Fri Jun 16, 2006 6:58 pm
by tecktalkcm0391
I've tried a lot of stuff but I can't see to get it to work yet I have something else on the same page that works for something else....

Posted: Mon Jun 19, 2006 10:02 pm
by Lightzero
I'm using

Code: Select all

<input type='text' onkeydown='event.keyCode=0;return event.keyCode; event.returnValue = false;' value='abc'/>
I want users can't change the contents of this text, but it doesn't work with code above...
I don't want use "disabled=true"...
Thanks for your help :D