Post navigation

code snippets

Try and stop images from being downloaded from website

Okay, I know this is something people with a little understanding of how a browser works could get around, but my client – a photographer – wanted to stop the casual visitor from downloading her large photos easily by using right-click or just dragging to the desktop or another tab.

I’m not keen on modifying browser behaviour, but this jQuery code just targets the images (and you could make it more specific) so links can still be right-clicked.

jQuery(document).ready(function($) {

		// stop the user right-clicking and dragging on images (using .live so that it works on lightbox images too.)
		$('body img').live("contextmenu mousedown",function(e){
		    return false;
		});
});

The jQuery .bind function worked well enough, but this is only called when the page loads, so any dynamically generated images – say in a lightbox – are still vulnerable. So I used the .live function instead and it works a treat.

The mousedown event is what I’m using to stop the images being dragged and the contextmenu event for the disabling of the right-click menu.

Of course this doesn’t actually prevent the downloading of images if a determined user switched off javascript or even just inspected the page source, but it works well enough without hindering the honest user.

Let me know if you know a better way though – I know that .live is deprecated in jQuery 1.7 so a more up-to-date alternative would be good!

About David Goodman

Leave a Reply

Your email address will not be published. Required fields are marked *

Please enable javascript to post a comment !