disable right click on images only?

Discussion in 'JavaScript' started by disgust, Dec 4, 2006.

  1. #1
    (yes, I know users can get past this-- doesn't matter in this specific instance)

    I'd like to disable right clicking on certain images, either pagewide or image-by-image. I don't want an annoying error popup, I'd just like nothing to happen at all. is that possible?

    anyone have a simple script handy that'd do this?

    if nothing's available I guess I'll ripoff flickr's spaceball.gif thing
     
    disgust, Dec 4, 2006 IP
  2. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i use this code on some of my pages.
    i put it just below the <body> tag.

    
    <script language=JavaScript>
    <!--
    
    //Disable right mouse click Script
    //By Maximus (maximus@nsimail.com) w/ mods by DynamicDrive
    //For full source code, visit http://www.dynamicdrive.com
    
    var message="";
    
    ///////////////////////////////////
    function clickIE4(){
    if (event.button==2){
    return false;
    }
    }
    
    function clickNS4(e){
    if (document.layers||document.getElementById&&!document.all){
    if (e.which==2||e.which==3){
    return false;
    }
    }
    }
    
    if (document.layers){
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown=clickNS4;
    }
    else if (document.all&&!document.getElementById){
    document.onmousedown=clickIE4;
    }
    
    document.oncontextmenu=new Function("return false")
    
    // --> 
    </script>
    
    Code (markup):
     
    lbalance, Dec 4, 2006 IP
    disgust likes this.
  3. disgust

    disgust Guest

    Messages:
    2,417
    Likes Received:
    133
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks, certainly a start to what I'm after. is there any reasonably easy way to make it impact images only though?
     
    disgust, Dec 4, 2006 IP
  4. franklinrony

    franklinrony Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    work with ff too? i try one but after alert box,show context menu
     
    franklinrony, Jul 5, 2009 IP
  5. zain654321

    zain654321 Peon

    Messages:
    202
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <SCRIPT LANGUAGE="JavaScript">

    <!-- This script and many more are available free online at -->
    <!-- The JavaScript Source!! http://javascript.internet.com -->

    <!-- Begin
    function right(e) {
    var msg = "Sorry, you don't have permission to right-click.";
    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.onmousedown = right;
    document.images.onmouseup = right;
    }
    }
    }
    // End -->
    </SCRIPT>
    </HEAD>

    <!-- STEP ONE: Copy the event handler into the BODY tag -->

    <BODY onLoad="trap()">

    <!-- Script Size: 0.81 KB -->
     
    zain654321, Jul 8, 2009 IP
  6. nirajkum

    nirajkum Active Member

    Messages:
    815
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #6
    thanks for the code
     
    nirajkum, Jul 9, 2009 IP