Is preventDefault not recognized by IE6?

D

Doug Lerner

In the following script:

function txKeyPressHandler(theEvent) {
var key = theEvent.which || theEvent.keyCode;

switch (key) {
case Event.KEY_RETURN:
txIMSendMsg();
var userAgent = window.navigator.userAgent;
if (userAgent.indexOf('MSIE 6.0') == -1) theEvent.preventDefault();
break;
}
}

if I take out the if clause, forcing the preventDefault to get called even
for IE6 it causes a JS error (just in IE6).

Any idea why?

Thanks,

doug
 
L

Lasse Reichstein Nielsen

Doug Lerner said:
if I take out the if clause, forcing the preventDefault to get called even
for IE6 it causes a JS error (just in IE6).

Any idea why?


IE doesn't implement DOM 2 Events.

Instead of trying to detect IE, use feature detection to detect
the preventDefault method:

if(theEvent.preventDefault) {
theEvent.preventDefault();
} else {
theEvent.returnValue = false; // IE branch - do more tests if
// that's not enough
}


/L
 
D

Doug Lerner

IE doesn't implement DOM 2 Events.

Instead of trying to detect IE, use feature detection to detect
the preventDefault method:

if(theEvent.preventDefault) {
theEvent.preventDefault();
} else {
theEvent.returnValue = false; // IE branch - do more tests if
// that's not enough
}

Thanks for your reply. Your idea seems like a more reliable way to avoid an
error due to a missing method.

Do you have any suggestions on how to accomplish what I was attempting to
accomplish there - which is getting IE6 to *not* go on and add the pressed
key to the field in which it was detected?

Thanks!

doug
 
D

Doug Lerner

IE doesn't implement DOM 2 Events.

Instead of trying to detect IE, use feature detection to detect
the preventDefault method:

if(theEvent.preventDefault) {
theEvent.preventDefault();
} else {
theEvent.returnValue = false; // IE branch - do more tests if
// that's not enough
}

Oh, I see what you are saying. Setting theEvent.returnValue to false
accomplishes the same thing in this case. And, in fact, it does. I just
tested it. Thanks! Very nice!

What does the preventDefault() do that setting returnValue to false not do?

doug
 
M

Martin Honnen

Doug Lerner wrote:

What does the preventDefault() do that setting returnValue to false not do?

The point is that on the web you encounter browsers with different event
models, in the beginning there was no event object having properties or
methods to prevent the default action associated with an event, you
would simply do e.g.
return true;
or
return false;
in the event handler function where for most events returning false
indicated that you don't want the default action (e.g. for link click
following the link) to be executed. Note however that there were a few
events (mousover/out, error) where returning false had the opposite meaning.
IE's event model usually allows doing return true/false as well but (I
think mostly to have an event model that is scriptable with more than
J(ava)Script) they introduced a global window.event property and allow
setting event.returnValue.
The W3C DOM Level 2 Events model additionally introduced the
preventDefault method on event objects to have a consistent and
language/binding independent way to prevent the default action
associated with and event.
While Mozilla and Opera by now support the W3C DOM Level 2 Events model
MSIE has never bothered to implement that.

So the only difference is that where preventDefault is supported you
have consistent way to prevent the default action by calling that method
while doing return true/false or event.returnValue = true/false for most
events prevents the default action with value false but for instance
mouseover/out on links to prevent the default action, showing of the
link href URL in the status bar, you need to return true. That case
however has become less important as most browsers by now can be
configured and might even configured by default do disallow scripts to
change the status bar message.
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top