event handler in <body> doesn't work

C

Chris

Hi.

I tried to disable the backspace-key using 2 different scenarios.
One works but the other doesn't and I don't understand why the 2nd
scenario doesn't work

Scenario 1 (working scenario)

<head>
<script language="javascript" type="text/javascript">
function DisableBackspace(e) {
// for complete code, see end of mail
}
// activating the handler here
document.onkeydown = DisableBackspace;
</script>
</head>

<body> <!-- i don't specify any handler in the body section
</body>


Scenario 2

<head>
<script language="javascript" type="text/javascript">
function DisableBackspace(e) {
// for complete code, see end of mail
}
// NOT activating the handler here
// document.onkeydown = DisableBackspace;
</script>
</head>

<body onkeydown="DisableBackspace(event);"> <!-- Activating here
instead -->
</body>

but the 2nd scenario doesn't work.
why not?

thank you
Chris


Complete code:

<html>
<head>
<title></title>

<script language="javascript" type="text/javascript">
function DisableBackspace(e) {
if (!e)
e = window.event;

if (e.keyCode)
keycode = e.keyCode; // IE
else
keycode = e.which; // Firefox

if (e.keyCode == 8)
return false;
}
document.onkeydown = DisableBackspace; // uncomment
for Scenario 1
</script>

</head>
<!-- <body onkeydown="DisableBackspace(event);"> --> <!-- uncomment
for Scenario 2: -->

<body>
<!-- uncomment for Scenario 1: -->
</body>
</html>
 
M

Martin Honnen

Chris said:
<body onkeydown="DisableBackspace(event);"> <!-- Activating here
instead -->
</body>

but the 2nd scenario doesn't work.
why not?

You need
<body onkeydown="return DisableBackspace(event);">
 
L

Lasse Reichstein Nielsen

Chris said:
I tried to disable the backspace-key using 2 different scenarios.
One works but the other doesn't and I don't understand why the 2nd
scenario doesn't work

Scenario 1 (working scenario)

<head>
<script language="javascript" type="text/javascript">

(The language attribute is unnecessary)
function DisableBackspace(e) {
// for complete code, see end of mail
}
// activating the handler here
document.onkeydown = DisableBackspace;
</script>
</head>

<body> <!-- i don't specify any handler in the body section
</body>


Scenario 2

<head>
<script language="javascript" type="text/javascript">
function DisableBackspace(e) {
// for complete code, see end of mail
}
// NOT activating the handler here
// document.onkeydown = DisableBackspace;
</script>
</head>

<body onkeydown="DisableBackspace(event);"> <!-- Activating here

onkeydown="return DisableBackspace(event);"
instead -->
</body>

but the 2nd scenario doesn't work.
why not?

thank you
Chris


Complete code:

<html>
<head>
<title></title>

<script language="javascript" type="text/javascript">
function DisableBackspace(e) {
if (!e)
e = window.event;
Remember to declare keycode as local:
var keycode;
if (e.keyCode)
keycode = e.keyCode; // IE
else
keycode = e.which; // Firefox

if (e.keyCode == 8)

Probably meant to check the keycode variable:
if (keycode == 8)
return false;
}
document.onkeydown = DisableBackspace; // uncomment
for Scenario 1
</script>

</head>
<!-- <body onkeydown="DisableBackspace(event);"> --> <!-- uncomment

Return the result here too.
for Scenario 2: -->

<body>
<!-- uncomment for Scenario 1: -->
</body>
</html>


Alternatively you can use the two other ways of cancelling the event
(IE and W3C DOM methods):

e.returnValue = false; // IE DOM
if (e.preventDefault) { e.preventDefault(); } // W3C DOM

Together they should stop the event just as efficiently as having
the handler return false in modern browsers and in IE.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top