Solved

Disable backspace-button

Posted on 2002-03-13
12
18,989 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
ID: 6861401
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
ID: 6861503
"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
ID: 6861730
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Expert Comment

by:petka
ID: 6862516
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
ID: 6862523
a
0
 
LVL 32

Author Comment

by:Batalf
ID: 6862535
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6862724
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
ID: 7906402
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
ID: 8078669
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
ID: 8204322
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
ID: 13224362
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
ID: 14602892
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

832 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