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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

COBOLdinosaurCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.