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.
-
You can only cancel specific events. You cannot "globally cancel" default actions.
To specifically cancel
draggingan image (which is only a default function in some browsers), returnfalseto themousedownevent.eyelidlessness : You 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.Doug Neiner : That is true, that would have nasty side effects, but is definitely more of a global concept.eyelidlessness : 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.Ale Anderson : I am not looking for cancelling all default actions. I asked for a way to cancel a default action just on an image.eyelidlessness : Ale Anderson, what "default action"? There are many actions available.Doug Neiner : @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. -
I have to say, the correct answer to this is use jQuery. All the browser-specific bugs and quirks and incompatibilities are all taken care of when you use jQuery.
Specifically, you would set up a custom event handler for the event(s) you would like to block, and call preventDefault in the event handler to prevent the default action from being taken.
Ale Anderson : jQuery is a wrong answer.Tim Down : That is not "the correct answer". It is a possible answer.Justin Johnson : The right answer is a "a" JavaScript library.Tim Down : @Justin: That is not "the right answer". It is a possible answer. -
You can register the events you want to cancel, and then either
return falsefrom them or useEvent.preventDefault(), depending on the browser and event.squiddle : this is not working cross-browser. But the poster hasn't specified which browsers are targeted anyway. -
(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.
0 comments:
Post a Comment