Batalf
asked on
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,textare a 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
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,textare
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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),
Batalf
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.
<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.
a
ASKER
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
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&
Cd&
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>
<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>
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()> ...
<script language=javascript>
function cancelBack()
{
if (event.keyCode == 8 && event.srcElement.form == null)
{
event.cancelBubble = true;
event.returnValue = false;
}
}
</script>
<body onkeydown=cancelBack()> ...
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.isTextEdi t == false)
)
{
event.cancelBubble = true;
event.returnValue = false;
}
}
</script>
<body onkeydown=cancelBack()> ...
<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.isTextEdi
)
{
event.cancelBubble = true;
event.returnValue = false;
}
}
</script>
<body onkeydown=cancelBack()> ...
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.onbeforeunlo ad = 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.
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.onbeforeunlo
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.
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... :(
The accepted solution needs many change in the code... :(
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.