?
Solved

Disable Backspace in Firefox

Posted on 2006-11-23
2
Medium Priority
?
1,751 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 500 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

649 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