Solved

Disable backspace-button

Posted on 2002-03-13
12
18,965 Views
Last Modified: 2011-08-18
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
0
Comment
Question by:Batalf
  • 2
  • 2
  • 2
  • +6
12 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 100 total points
Comment Utility
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
 
LVL 19

Expert Comment

by:webwoman
Comment Utility
"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
 
LVL 32

Author Comment

by:Batalf
Comment Utility
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
 

Expert Comment

by:petka
Comment Utility
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
 

Expert Comment

by:petka
Comment Utility
a
0
 
LVL 32

Author Comment

by:Batalf
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
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
 

Expert Comment

by:UB40
Comment Utility
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
 

Expert Comment

by:elvin226
Comment Utility
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
 

Expert Comment

by:richball
Comment Utility
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
 

Expert Comment

by:ThomasUmmels
Comment Utility
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
 

Expert Comment

by:tapika
Comment Utility
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now