View Full Version : onkeyup and backspace key


huseyin_akturk
06-12-2005, 01:56 AM
Hi,
Firstly, I want to say I am new on javascript.
I want to remove backspace key. I mean that when user presses backspace key, I will prevent user to go back page. But user can press any part of the page, not speficific part. I thought that I can do this operation in body tag. And I wrote this code;
<body onkeyup="blockbackspace(this)">
And there is a javascript function;
function blockbackspace(n){
if (event.keyCode == 8){
????
}
}
As you see above, when user press backpace "if (event.keyCode == 8)" is true. But what should I do rest of this? Altough I wrote this javascript the back page is loaded. How can I prevent?

SEO Aaron
06-14-2005, 09:24 PM
This seems to work.

<html>
<head>
<title>Remove Backspace</title>
<script type="text/javascript">
var allow_backspace = false;
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

function trapKeyPress(event)
{
if (!allow_backspace && keyCode == 8)
return false;
return true;

}
</script></head>
<body onkeydown="return trapKeyPress(event);" onkeypress="return trapKeyPress(event);">



</BODY>
</HTML>

I found it here....modified it a bit.

http://groups-beta.google.com/group/comp.lang.javascript/browse_thread/thread/aacb48908b4cd202/a4e76e3a0aca5407?q=onkeypress+backspace&rnum=2#733b6bdc06b664f9