By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,186 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,186 IT Pros & Developers. It's quick & easy.

Can't add return false with addEventListener in Firefox

P: n/a
Hi,

Does anyone know why I can't add return false with addEventListener in
firefox (1.0.6).
This demonstrates the problem
If return false is added to onmousedown then you can drag over the
image which you can do to myImg1 but not myImg2.
myImg3 just shows that the code should work as an alert is added to
this one.

<img name="myImg1" id="myImg1"
src="http://www.google.co.uk/intl/en_uk/images/logo.gif"
onmousedown="return false">
<img name="myImg2" id="myImg2"
src="http://www.google.co.uk/intl/en_uk/images/logo.gif">
<img name="myImg3" id="myImg3"
src="http://www.google.co.uk/intl/en_uk/images/logo.gif">

<script>
function doOnload(){
document.getElementById('myImg2').addEventListener ('mousedown',function(){return
false;},true);
document.getElementById('myImg3').addEventListener ('mousedown',function(){alert('down');},true);
}

window.addEventListener('load',doOnload,true);
</script>

Thanks anyone who can help.

Aug 8 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
philjhanna wrote:
Does anyone know why I can't add return false with addEventListener in
firefox (1.0.6).


Because Firefox/Mozilla will ignore any result returned from a function
registered with addEventListener. If you read the DOM specification for
the EventListener interface you will see that it says there is 'no return value'.

See the following references:

http://www.mozilla.org/docs/dom/domr..._el_ref31.html
http://www.w3.org/TR/2000/REC-DOM-Le...-EventListener

or more specifically
(at http://www.w3.org/TR/2000/REC-DOM-Le...binding.html):

Object EventTarget

The EventTarget object has the following methods:

addEventListener(type, listener, useCapture)
This method has no return value.
The type parameter is of type String.
The listener parameter is a EventListener object.
The useCapture parameter is of type Boolean.
removeEventListener(type, listener, useCapture)
This method has no return value.
The type parameter is of type String.
The listener parameter is a EventListener object.
The useCapture parameter is of type Boolean.
dispatchEvent(evt)
This method returns a Boolean.
The evt parameter is a Event object.
This method can raise a EventException object.

Object EventListener
This is an ECMAScript function reference. This method has no return value.
The parameter is a Event object.

Aug 9 '05 #2

P: n/a


philjhanna wrote:

Does anyone know why I can't add return false with addEventListener in
firefox (1.0.6).
This demonstrates the problem
If return false is added to onmousedown then you can drag over the
image which you can do to myImg1 but not myImg2. <img name="myImg1" id="myImg1"
src="http://www.google.co.uk/intl/en_uk/images/logo.gif"
onmousedown="return false">
<img name="myImg2" id="myImg2"
src="http://www.google.co.uk/intl/en_uk/images/logo.gif">
document.getElementById('myImg2').addEventListener ('mousedown',function(){return
false;},true);


The proper way in the W3C DOM Level 2 Events model to cancel the default
action associated with an event is to call the method preventDefault on
the event object so you should code
document.getElementById('myImg2').addEventListener (
'mousedown',
function (evt) {
if (evt.preventDefault) {
evt.preventDefault();
}
},
false
);
if you want to have that event listener cancel/prevent the default action.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 9 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.