Can't add return false with addEventListener in Firefox

P

philjhanna

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.
 
M

Martin Honnen

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.
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top