PDA

View Full Version : How to disable 'Backspace' key in IE7



me_myself
07-21-2008, 03:33 PM
My companies intranet needs the 'Backspace' key to be disabled. Can someone please let me know how to disable backspace key in IE7.

Thanks

Dal
07-21-2008, 03:53 PM
I was working on how to disable enter key so the people who answered my thread might provide the answer.

http://www.dynamicdrive.com/forums/showthread.php?t=34561

Kind regards
Dal

jscheuer1
07-21-2008, 04:13 PM
Yes, this works in IE 7 (others):


<script type="text/javascript">
function killBackSpace(e){
e = e? e : window.event;
var k = e.keyCode? e.keyCode : e.which? e.which : null;
if (k == 8){
if (e.preventDefault)
e.preventDefault();
return false;
};
return true;
};
if(typeof document.addEventListener!='undefined')
document.addEventListener('keydown', killBackSpace, false);
else if(typeof document.attachEvent!='undefined')
document.attachEvent('onkeydown', killBackSpace);
else{
if(document.onkeydown!=null){
var oldOnkeydown=document.onkeydown;
document.onkeydown=function(e){
oldOnkeydown(e);
killBackSpace(e);
};}
else
document.onkeydown=killBackSpace;
}
</script>

But it will disable its use in form fields as well as for activating the browser's back button function. The srcElement could be tested for though, that could allow it to still work normally in forms. Let me know if that is a concern.

me_myself
07-21-2008, 04:22 PM
jscheuer1, thanks a lot for the quick reply.

Never did it strike my mind. What if the user wants to delete a value from the textbox (form) would the backspace work in that case? Can i have the backspace work inside a form (when focus is in a control) and when the focus is out somewhere, the backspace disabled?? Can this be done?

Thanks again.

jscheuer1
07-21-2008, 04:49 PM
Sure, though focus in a form isn't the best criteria because focus could be on a button in a form, in which case backspace would still activate the browser's back button function. As far as I can tell only text inputs, password inputs, file inputs, and textareas should be given an exception:


<script type="text/javascript">
function killBackSpace(e){
e = e? e : window.event;
var t = e.target? e.target : e.srcElement? e.srcElement : null;
if(t && t.tagName && (t.type && /(password)|(text)|(file)/.test(t.type.toLowerCase())) || t.tagName.toLowerCase() == 'textarea')
return true;
var k = e.keyCode? e.keyCode : e.which? e.which : null;
if (k == 8){
if (e.preventDefault)
e.preventDefault();
return false;
};
return true;
};
if(typeof document.addEventListener!='undefined')
document.addEventListener('keydown', killBackSpace, false);
else if(typeof document.attachEvent!='undefined')
document.attachEvent('onkeydown', killBackSpace);
else{
if(document.onkeydown!=null){
var oldOnkeydown=document.onkeydown;
document.onkeydown=function(e){
oldOnkeydown(e);
killBackSpace(e);
};}
else
document.onkeydown=killBackSpace;
}
</script>

Let me know if any other elements should qualify for exception.

me_myself
07-21-2008, 04:59 PM
Thanks a lot jscheuer1

me_myself
07-22-2008, 06:38 PM
jscheuer1, my developers have .NET Atlas form validation code in it and they say it is causing conflict with this code and as a result the backspace code is not working. Do you know of any work around on how to get this code work along with the other?

Thanks.

jscheuer1
07-22-2008, 11:24 PM
There really shouldn't be any conflict unless the code you are speaking about already has a function named killBackSpace, as that is the only global in the code. However, I know nothing about .NET Atlas. It should be server side (at least most .NET code is), and so should not technically conflict. It may do things that render the code useless though. I would have to see the page where this is happening to even have a chance at determining what the problem is.

Please post a link to the page on your site that contains the problematic code so we can check it out.

vickytulla
11-29-2010, 01:19 PM
yes it really works in IE...

santoshreddy.spy
02-24-2012, 09:16 AM
Yes, this works in IE 7 (others):


<script type="text/javascript">
function killBackSpace(e){
e = e? e : window.event;
var k = e.keyCode? e.keyCode : e.which? e.which : null;
if (k == 8){
if (e.preventDefault)
e.preventDefault();
return false;
};
return true;
};
if(typeof document.addEventListener!='undefined')
document.addEventListener('keydown', killBackSpace, false);
else if(typeof document.attachEvent!='undefined')
document.attachEvent('onkeydown', killBackSpace);
else{
if(document.onkeydown!=null){
var oldOnkeydown=document.onkeydown;
document.onkeydown=function(e){
oldOnkeydown(e);
killBackSpace(e);
};}
else
document.onkeydown=killBackSpace;
}
</script>

But it will disable its use in form fields as well as for activating the browser's back button function. The srcElement could be tested for though, that could allow it to still work normally in forms. Let me know if that is a concern.
1) Hw to disable back button on browser after logout?
2) and also hw to disable refresh buton or kill the previous cookie bafore generating a new one?