SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Right-hand mouse click disable on image only

    Hi

    I've searched though the forum and can't find an exact solution for disabling the right-hand mouse-click only on images (not text) on a webpage. I have some code that will do the job but I need some help with it. I realise that you can't stop people taking anything from webpages, so the aim of the following code is to provide an inoffensive way of reminding people that images have a copyright, and it leaves the right-hand mouse-click working for the rest of the page.

    How do I change the following js code to run completely in an external js file? I already have the following code in an external file, but for this code to work each XHTML page has to have <body onload="trap()"> placed in it. I want to get rid of the onload="trap()" bit because the XHTML files do not validate though the W3C validation system with it placed in the page.


    function right(e)
    {
    var msg = "To copy/use this image, please contact the copyright holder for permission.";
    if (navigator.appName == 'Netscape' && e.which == 3) {
    alert(msg);
    return false;
    }
    if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2) {
    alert(msg);
    return false;
    }
    else return true;
    }

    function trap()
    {
    if(document.images)
    {
    for(i=0;i<document.images.length;i++)
    {
    document.images[i].onmousedown = right;
    document.images[i].onmouseup = right;
    }
    }
    }

  2. #2
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code error in my message

    Hi

    That message should read: "to copy or use..."

    not: "to copy/use..."

    Cheers

  3. #3
    Guru Meditation Error gnarly's Avatar
    Join Date
    Dec 2003
    Location
    Cheltenham, United Kingdom
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a completely pointless waste of time and it's nearly impossible to make it work across all browsers. Someone who wants the image will always find a way around it - view source is usually a good one, switching off JS is another easy one. If you're really that worried about your images, simply don't put the images on the web. Why not just put a copyright message in the HMTL? It'd be just as effective and much easier to update whenever a new browser broke your script (i.e. you wouldn't need to).

    You might want to google addLoadEvent to see how to fire JS functions onload.
    Olly Hodgson
    thinkdrastic.net

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Why not switch to the Opera web browser. If everyone does that then this stupid annoying no right click issue will go away because that browser sensibly ignores any such script.

    No one who is serious about stealing other people's images uses the right mouse button to do it as there are many much quicker and easier ways that can't be blocked.

    The only effective protection for images is to embed a watermark in the image itself.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •