Solved

Disable Backspace in Firefox

Posted on 2006-11-23
2
1,736 Views
Last Modified: 2012-05-05
I want to disable the backspace button from being used as a back button when the user is not in a textarea or input field.  I am using the below code right now, but it only works in IE and has no affect in Firefox.

<script language="javascript">
var inform = false;
document.onkeydown = keyCatcher;

function keyCatcher() {
      var e = event.srcElement.tagName;

      if (event.keyCode == 8 && e != "input" && e != "textarea") {
            event.cancelBubble = true;
            event.returnValue = false;
      }
}
</script>
0
Comment
Question by:jls33fsls
2 Comments
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 125 total points
ID: 18006295
Hi,

In Firefox you need to catch the onkeypress event. Try this sort of thing. This should work for IE and Firefox/Mozilla/Netscape. I haven't tried it as I am not a machine that I can try it out on at the moment. This checks what the element type is and if it is an input then it allows the backspace. With a bit of tweaking it could be made to work with textareas as well.


<script type="text/javascript">

if (typeof window.event != 'undefined')
document.onkeydown = function()
{
if (event.srcElement.tagName.toUpperCase() != 'INPUT')
return (event.keyCode != 8);
}
else
document.onkeypress = function(e)
{
if (e.target.nodeName.toUpperCase() != 'INPUT')
return (e.keyCode != 8);
}

</script>


Cheers,

Lee
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18006311
> var e = event.srcElement.tagName;
is old IE only
w3c compliant browser use  event.target, sometimes event.view is also possible

AFAIK cancelBubble is IE proprietary too, but IE as gecko based browser (mozilla, firefox) do not comply to w3c defined standard how to propagate the event up and down the object tree. So be prepared for some ugly hacks if you need that.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now