Disable backspace-button

Hello

I need to know how to disable the backspace-button for a web-page,ie: don't make the browser take one step back in the browser history.

BUT: I need to be able to use the backspace in form-elements(text,textarea etc.).

Right now, I have managed to disable the backspace-button for the entire page with this code

<script>
function checkShortcut()
{
                  if(event.keyCode==8 || event.keyCode==13)
         {
               return false;
         }
}
 </script>
<body onkeydown="return checkShortcut()">

But then I'm unable to use the backspace-button to delete characters from the form.

Batalf
LVL 32
BatalfAsked:
Who is Participating?
 
COBOLdinosaurConnect With a Mentor Commented:
You could set and clear a variable with onFocus and onBlur events then test the var in the script.

<input type="text" onFocus="x=true" onBlur="x=false">

Then in the script add if (x) return;

Cd&
0
 
webwomanCommented:
"I need to know how to disable the backspace-button for a web-page,ie: don't make the browser take one
step back in the browser history."

Disabling the backspace key doesn't do anything about the back button of the browser, and people are way more likely to use that.

It would be better to just replace your entry in the history. Check some of the PAQs, I know it's been covered numerous times.
0
 
BatalfAuthor Commented:
Cd&: Your solution is the way I was thinking too,but I was hoping that it was possible of doing this a simpler way.

webwoman:
This is not an ordinary webpage, but a webapplication on an intranet for users running IE5.0(a controlled environment). The problem with the backbutton is that when the user is writing an article in this application(DHTML-editor), he use the backbutton to delete characters from the textfield. BUT: If the editor doesn't have focus(maybe without the users knowledge), a press on the backspace-button will send the user back one step in the history(to the login-screen). Then he loses all his work and get extremly frustrated:-). That's the major problem I want to solve. I'm not trying to prevent the user from going back in the history with the "Back"-button.

Batalf
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
petkaCommented:
just do this without any javascript
<textarea readonly disable> some text</textarea>

I believe you need both "readonly" and "disable" words so it would work properly in both Netscape and Explorer.

0
 
petkaCommented:
a
0
 
BatalfAuthor Commented:
I don't want to disable any form-fields. As I wrote in my question, I'm looking for the best way of disabling the BACKSPACE-button when the focus is not in any form-fields. All the form-fields(textarea,text etc.) should work as usual
0
 
COBOLdinosaurCommented:
Thanks for the A Batalf.  :^) I wish there was something a little more elegant, but I couldn't find anything that would selectively disable without having to use focus and blur; but at least there is something to keep the users from getting frustrated.

Cd&
0
 
UB40Commented:
Here's a bit of VBScript which will do the same, plus disable the context menu. You could probably translate this into Javascript if you prefer:-

<SCRIPT LANGUAGE="VBScript">
 
Sub document_onkeydown()

        set we = window.event
     ' disable backspace when not in a text or textarea
     if we.keyCode = 8 then
          if we.srcElement.tagName <> "INPUT" and we.srcElement.tagName <> "TEXTAREA" then
               we.returnvalue  = false
               we.cancelbubble = true
          end if              
     end if

        if we.altKey then
            if we.keycode = 37 then
             we.returnvalue  = false
             we.cancelbubble = true
            end if
        end if

    End Sub


    Sub document_oncontextmenu()

     set we = window.event
     we.returnValue  = false
     we.cancelBubble = true

    end Sub
</SCRIPT>
0
 
elvin226Commented:
aside from knowing whether the source element is an input or a textarea, it would be best to know if it is embedded inside a form. In your case, you want to cancel the backspace when the focus is not in a form element (event.srcElement.form == null). Try this code:

<script language=javascript>

  function cancelBack()
  {
    if (event.keyCode == 8 && event.srcElement.form == null)
    {
      event.cancelBubble = true;
      event.returnValue = false;
    }
  }

</script>

<body onkeydown=cancelBack()> ...
0
 
richballCommented:
I made some modifications to the previous suggestion that support disabling the backspace button on radio controls and selection boxes.  I also added the Alt-Left Arrow and Alt-Right Arrow to the list of keys to not process.


<script language=javascript>
         
    function cancelBack()
    {
        if ((event.keyCode == 8 ||
           (event.keyCode == 37 && event.altKey) ||
           (event.keyCode == 39 && event.altKey))
            && 
           (event.srcElement.form == null || event.srcElement.isTextEdit == false)
          )
        {
            event.cancelBubble = true;
            event.returnValue = false;
        }
    }

</script>

<body onkeydown=cancelBack()> ...
0
 
ThomasUmmelsCommented:
I am struggling with the same problem.....
I like this approach:
                                          
function funcBodyBeforeOnUnload() {
      event.returnValue = 'put your message here to tell the user what's happening'
      }

and add this line to your script on a global level after you declared the function

document.body.onbeforeunload = funcBodyBeforeOnUnload;      

When ever the user leaves the page you can ask the user if he first wants to save his work or finish what he is doing. This way every possible incident is covered.
0
 
tapikaCommented:
richball's solution is nice, because its a global solution, not necessary to change all the input statements. But its not working with Netscape. I tested on IE6 and Netscape 7.2, and its working with IE6 only.

The accepted solution needs many change in the code... :(
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.