ondrag event equivalent for mozilla

D

David

Is there something that I can use to prevent the a user from dragging
something, an image for instance. In IE I can use the ondrag = "return
false;", is there a way to achieve the same way with the other browsers?

Thanks,

David
 
M

Martin Honnen

David said:
Is there something that I can use to prevent the a user from dragging
something, an image for instance. In IE I can use the ondrag = "return
false;", is there a way to achieve the same way with the other browsers?

<img onmousedown="if (event.preventDefault) {
event.preventDefault();
}
return false;"
src="whatever.gif" alt="whatever">
should do.
 
D

David

Martin Honnen:
<img onmousedown="if (event.preventDefault) {
event.preventDefault();
}
return false;"
src="whatever.gif" alt="whatever">
should do.

--

The problem with this is that I have the img tag nested inside a div tag
which needs to be clicked and not have the event cancelled. Any other
ideas?
 
M

Martin Honnen

David said:
Martin Honnen:


The problem with this is that I have the img tag nested inside a div tag
which needs to be clicked and not have the event cancelled. Any other
ideas?

You can still click the <div> outside of the <img>.
 
D

David

Martin said:
You can still click the <div> outside of the <img>.

Ok, so let me clarify what I meant, I have the img tag nest inside a div tag
and the img tag click event NEEDS to bubble and not be cancelled. So is
there any other way to go about cancelling drag? If not, it's no big dealy,
just would be nice.

Thanks!
 
A

ASM

David said:
so let me clarify what I meant, I have the img tag nest inside a div tag
and the img tag click event NEEDS to bubble and not be cancelled. So is
there any other way to go about cancelling drag? If not, it's no big dealy,
just would be nice.

and adding

onmouseup="this.click();"

would it run ?
 
D

David

ASM said:
and adding

onmouseup="this.click();"

would it run ?

Heh, no, the mousedown, mouseup, and click events all need to bubble up to
the DIV tag, any other ideas? Thanks for bearing with me!

David
 
M

Martin Honnen

David wrote:

Ok, so let me clarify what I meant, I have the img tag nest inside a div tag
and the img tag click event NEEDS to bubble and not be cancelled. So is
there any other way to go about cancelling drag?

Well the event continues to bubble, what I suggested is to use
preventDefault() which prevents the default action but not
stopPropagation() which would prevent the event propagation.

Here is a short and simple test case for Mozilla and Opera 8:

<html lang="en">
<head>
<title>difference between preventDefault and stopPropagation</title>
<script type="text/javascript">
function output (tagName, text) {
var element = document.createElement(tagName);
element.appendChild(document.createTextNode(text));
document.body.appendChild(element);
}
</script>
</head>
<body>

<h1>difference between preventDefault and stopPropagation</h1>

<h2>div element whith image which should not allow dragging</h2>

<div id="div1"
style="border: 1px solid green;"
onmousedown="output('p', 'div handles ' + event.type);"
onclick="output('p', 'div handles ' + event.type);"
onmouseup="output('p', 'div handles ' + event.type);">
<p>Here is a div with an img element where the normal drag operation the
browser offers is prevented:
<img src="kiboInside.gif" alt="Kibo inside"
onmousedown="if (event.preventDefault) {
event.preventDefault();
}">
</p>
</div>

<hr>

<h2>debug output of events</h2>

</body>
</html>


You will find that the <div> element containing the <img> will get any
events as events still propagate.

I think in my first answer I had also some return false in the event
handler but that should then not be used.
 
A

ASM

Martin Honnen wrote:

[snip interesting example page]

works fine with FF, Safari, Opera, Camino
but
IE5.2 (Mac), iCab3 yet allowes to catch (and drag) image ...
 
M

Martin Honnen

ASM said:
but
IE5.2 (Mac), iCab3 yet allowes to catch (and drag) image ...

For IE that is obvious as the example code only uses W3C DOM Level 2
Events code that neither IE on the Mac nor IE on Win support.
For IE/Win the original poster, David, already has a solution, I am not
sure off hand whether IE/Mac supports the ondrag handler and cancelling
the drag event.
No idea about iCab, I don't have it and there is not a lot of
documentation, I guess it might not support the event.preventDefault
method.
 
D

David

Martin said:
David wrote:



Well the event continues to bubble, what I suggested is to use
preventDefault() which prevents the default action but not
stopPropagation() which would prevent the event propagation.

Here is a short and simple test case for Mozilla and Opera 8:

<html lang="en">
<head>
<title>difference between preventDefault and stopPropagation</title>
<script type="text/javascript">
function output (tagName, text) {
var element = document.createElement(tagName);
element.appendChild(document.createTextNode(text));
document.body.appendChild(element);
}
</script>
</head>
<body>

<h1>difference between preventDefault and stopPropagation</h1>

<h2>div element whith image which should not allow dragging</h2>

<div id="div1"
style="border: 1px solid green;"
onmousedown="output('p', 'div handles ' + event.type);"
onclick="output('p', 'div handles ' + event.type);"
onmouseup="output('p', 'div handles ' + event.type);">
<p>Here is a div with an img element where the normal drag operation the
browser offers is prevented:
<img src="kiboInside.gif" alt="Kibo inside"
onmousedown="if (event.preventDefault) {
event.preventDefault();
}">
</p>
</div>

<hr>

<h2>debug output of events</h2>

</body>
</html>


You will find that the <div> element containing the <img> will get any
events as events still propagate.

I think in my first answer I had also some return false in the event
handler but that should then not be used.

Ok, now I understand better, not sure why I thought that preventDefault()
kept the event from bubbling. Thanks a lot for the help!

David
 

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top