What is the cross-browser method? I need to prevent any default action on an image, so that neither dragging nor anything else will fire on a default bases.
-
All is clear. Read carefully. – Ale Anderson Dec 11 '09 at 07:19
-
4Ale Anderson, if people ask you to clarify your question you might consider that what seems clear to you isn't necessarily clear to others. Your question is extremely unclear. – eyelidlessness Dec 11 '09 at 08:10
-
2At least define what the "default action on an image" is. This is new to me. – Crescent Fresh Dec 11 '09 at 11:32
-
1http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false – OrangeDog Jan 07 '13 at 15:19
3 Answers
You can register the events you want to cancel, and then either return false
from them or use Event.preventDefault()
, depending on the browser and event.

- 1,434
- 9
- 13
-
this is not working cross-browser. But the poster hasn't specified which browsers are targeted anyway. – squiddle Dec 11 '09 at 10:28
(function() {
var onmousedown;
if('onmousedown' in document && typeof document.onmousedown == 'function') {
onmousedown = document.onmousedown;
}
document.onmousedown = function(e) {
if(typeof e == 'undefined') {
e = window.event;
}
if(!e.target) {
e.target = e.srcElement || document;
}
if('nodeName' in e.target && e.target.nodeName.toLowerCase() == 'img') {
if(e.preventDefault) {
e.preventDefault();
}
// If you want to register mousedown events for
// elements containing images, you will want to
// remove the next four lines.
if(e.stopPropagation) {
e.stopPropagation();
}
e.cancelBubble = true;
e.returnValue = false;
return false;
}
if(onmousedown !== undefined) {
onmousedown(e);
}
};
})();
You may need to do something similar to other events you'd like to prevent, if this doesn't do what you want.
Also it's worth noting that if you're trying to prevent people from downloading images from a page to their computer, you will not succeed. If a browser can download an image, so can the user. Using JavaScript to block (some) attempts is easily circumvented by simply disabling JavaScript.

- 62,413
- 11
- 90
- 94
You can only cancel specific events. You cannot "globally cancel" default actions.
To specifically cancel dragging
an image (which is only a default function in some browsers), return false
to the mousedown
event.

- 65,509
- 13
- 109
- 118
-
2You can globally cancel a lot of default actions, but not all of them. In this case (forgive the awful code) `document.onmousedown = function(e) { if(typeof e == 'undefined') e = window.event; if(e.preventDefault) e.preventDefault(); if(e.stopPropagation) e.stopPropagation(); e.returnValue = false; return false; };` But obviously this will have tons of nasty side-effects. – eyelidlessness Dec 11 '09 at 07:09
-
That is true, that would have nasty side effects, but is definitely more of a global concept. – Doug Neiner Dec 11 '09 at 07:10
-
To add: that works on pretty much any event that bubbles. There has been some progress for "event delegation" (obviously this is an overlapping concept and not exactly 1:1) with non-bubbling events, but I don't really know how these are implemented. I know jQuery's recent alpha has started to implement these, so it may be worth a look. – eyelidlessness Dec 11 '09 at 07:14
-
I am not looking for cancelling all default actions. I asked for a way to cancel a default action just on an image. – Ale Anderson Dec 11 '09 at 07:18
-
1Ale Anderson, what "default action"? There are many actions available. – eyelidlessness Dec 11 '09 at 08:13
-
@Ale, you actually asked that neither that default action "nor anything else", and the title of the question references "actions" plural. If you mean only one, then you should edit your question to be accurate to your needs. – Doug Neiner Dec 11 '09 at 13:46