Solved

Disable Backspace in Firefox

Posted on 2006-11-23
2
1,739 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

911 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

27 Experts available now in Live!

Get 1:1 Help Now