Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2007
    Posts
    100
    Thanks
    16
    Thanked 12 Times in 12 Posts

    How to set onContextMenu for image?

    I am trying to disable the menu which appears when a user right-clicks on an image. Using the onContextMenu directly in the IMG tag like this seems to do the trick.

    Code:
    <img src="/graphics/dotgrey.gif" width="25" height="25" onContextMenu="return(false)">

    However, setting this dynamically with Javascript does not seem to work.

    Code:
    <img src="/graphics/dotgrey.gif" width="25" height="25" id="myimage">
    
    <script language="JavaScript" type="text/javascript">
    
    document.getElementById('myimage').onContextMenu = 'return(false)';
    
    </script>

    Can anyone suggest a correct way of doing this with Javascript?
    Last edited by >ssp-cdr<; 10-09-2008 at 12:51 AM.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    First, though it has nothing to do with the script, remove the language attribute. It's deprecated.

    For your problem, try this:
    Code:
    window.onload=function()
    	{
    	document.getElementById('myimage').oncontextmenu = function()
    		{
    		return false; // Cancel any event from this point forward
    		} 
    	}
    ...also remember that JS is case-sensitive. onContextMenu is different from oncontextmenu

    Hope that helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    >ssp-cdr< (10-09-2008)

  • #3
    Regular Coder
    Join Date
    May 2007
    Posts
    100
    Thanks
    16
    Thanked 12 Times in 12 Posts
    works, thanks

  • #4
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What if I intend to do it for all images?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by simulationelson View Post
    What if I intend to do it for all images?

    Code:
    <script type = "text/javascript">
    function protect(e) {
    return false;
    }
    
    function trap() {
    for(i=0;i<document.images.length;i++)
    document.images[i].onmousedown = protect;
    }
    }
    </script>
    
    <body onload="trap()">
    Last edited by Philip M; 12-28-2008 at 02:57 PM.

  • #6
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It did the trick, all right! Thank you very much Mr. Philip M!

    I have this follow-up: Is it possible for the function to work only on images under a certain class (CSS)?

    Edit: The code works in both Firefox and IE when I'm offline. However, when I upload the files, it no longer works in IE.
    Last edited by simulationelson; 12-28-2008 at 07:47 PM.


  •  

    Posting Permissions

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