Disable image dragging in FireFox

FireFox 3 has a behaviour where clicking on image (even background image) and moving your mouse will start to drag that image around. Actually, when you drag an image, the browser thinks you want to save it to the desktop, so it starts default dragging action. To disable image dragging in FireFox, you will have to prevent default action on mousedown event for the image. Please try to drag left and right image.

Default behaviour
drag me
Dragging disabled
drag me if you can

<!-- left image (default behaviour) -->
<img src="spider1.png">

<!-- right image (dragging disabled) -->
<img src="spider2.png" onmousedown="if (event.preventDefault) event.preventDefault()">

If you write event.preventDefault() only (without testing), then IE will throw an error. Disabling, or event cancelation is accomplished by calling the Event's preventDefault method. preventDefault cancels the event if it is cancelable, without stopping further propagation of the event.

Small tip, but it can save your time.
;)

Bookmark and Share

6 Responses to “Disable image dragging in FireFox”

  1. Tacyt says:

    Thank You :)
    Keep up the good work!

  2. setekh says:

    thanks, nice tip

  3. Jim Johnson says:

    Thanks for the help however, the protection fails if JavaScript is disabled!
    Is their a CSS property that can be used instead?

  4. Jeff says:

    Works great for firefox, any idea how to do the same thing in IE without throwing script errors?

  5. dbunic says:

    @Jeff - You can place if (event.preventDefault) before calling event.preventDefault() and IE will not throw any error. Fortunately it's very easy to write a cross-browser JS code ... just test object before using. ;)

  6. Kirthika says:

    It sure did save lot of time. Thanks for taking time to share this.

Leave a Reply