?
Solved

Disable Backspace in Firefox

Posted on 2006-11-23
2
Medium Priority
?
1,749 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

777 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