Solved

Disable Backspace in Firefox

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

809 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