Paul McFedries' Web Home


JavaScript Programs

Preventing Right-Clicks

It's dismayingly easy to grab an image from a Web page: just right-click the image and then click Save Picture As (in Internet Explorer) or Save Image As (in Netscape). If your page uses original artwork, you almost certainly don't want other people purloining your hard work. One deterrent is to always include a copyright notice on each page. Another is to use JavaScript to prevent right-clicks. For example, try right-click this page and see what happens (note that I've been told that this doesn't work on the Mac OS; it also doesn't work if you click both buttons at the same time).

To add this feature to your pages, insert the following code between the </HEAD> and </BODY> tags:

<script language="JavaScript" type="text/javascript">
<!--
if (document.all) {
}
else if (document.getElementById) { 
    document.captureEvents(Event.MOUSEDOWN)
}
else if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN)
}

document.onmousedown = mousedown_handler

function mousedown_handler(mouse_event) {

    // This is the message that will appear
    var no_right_click = "Sorry, right-clicking is not allowed!"
    
    if (document.all) {

        //Probably Internet Explorer 4 and later
        if (event.button == 2 || event.button == 3) {
            alert(no_right_click)
            return false
        }
    }
    else if (document.getElementById) { 

        // Probably Netscape 6 and later
        if (mouse_event.which == 3) {
            alert(no_right_click)
            return false
        }
    }
    else if (document.layers) {

        // Probably Netscape 4
        if (mouse_event.which == 3) {
            alert(no_right_click)
            return false
        }
    }
}


//-->
</SCRIPT>
Bear in mind that this isn't a total solution. Savvy users will know that they can examine your source code, get the name and location of the image, and then enter the image's URL into the browser. Still, this method is good for dissuading the un-savvy.

If you want to go even further, you can put a "digital watermark" on your page. See http://www.digimarc.com/.


Copyright © 1995-2008 Paul McFedries and Logophilia Limited