473,396 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Is preventDefault not recognized by IE6?

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

Apr 20 '06 #1
4 9906
Doug Lerner <do**@lerner.net> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 20 '06 #2

On 4/20/06 3:18 PM, in article 8x**********@hotpop.com, "Lasse Reichstein
Nielsen" <lr*@hotpop.com> wrote:
Doug Lerner <do**@lerner.net> writes:
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
}


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

Apr 20 '06 #3

On 4/20/06 3:18 PM, in article 8x**********@hotpop.com, "Lasse Reichstein
Nielsen" <lr*@hotpop.com> wrote:
Doug Lerner <do**@lerner.net> writes:
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
}


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

Apr 20 '06 #4


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I have a textbox where I'm trying to restrict which keys a user can hit. My syntax is onKeyDown="myFunction(event)" To restrict the keys, Netscape requires the event.preventDefault method. ...
3
by: Michael Powe | last post by:
Hello, I am trying to work out the usage of preventDefault() and not having any success. The scenario is that I want to add on onclick handler to a link. When the onclick handler fires, I...
3
by: rss | last post by:
SUMMARY: ========== I am unable to pass along a simple Cookie obtained from a HttpWebRequest call (Machine A's ASP.NET app) so that another Web Server (Machine B) recongnizes the cookie. I...
4
by: Ê÷Éϲä»Ò | last post by:
'rar' is not recognized as an internal or external command, operable program or batch file. import os import time source = target_dir = r'e:\temp\bak' target =...
10
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not...
1
by: petermichaux | last post by:
Hi, The Yahoo! UI event library goes to extremely great lengths to solve this problem. Their solution is very creative but uses browser sniffing. In Safari 1.3 (and earlier?) the following...
1
by: Charlie | last post by:
I want to detect and act on right button clicks in Firefox. The problem was I kept getting the context menu pop up on right button clicks, even when I called event.preventDefault(). I had assumed...
2
by: ruthiefy | last post by:
Hi, I need a few sql queries results so I created a new class with has a few functions that load the data. After executing ExecuteReader, I need to load the data to the dropdownlist control....
0
by: fantasticamir | last post by:
I have a simple program using a couple of classes and it compiled successfully but while it is linking I have the following problem: g++ -Wall -lcu -lglu ACL2MDD.o Link.o Rule.o reachability.o ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.