Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Disable right mouse click context menu only on images.

Posted on 2011-03-09
7
525 Views
Last Modified: 2012-05-11
Hi,

I have this code to disable right mouse click on my pages and it works fine, however I need to modify it, so it disables only context menu on images, rest of the site should have right click enabled - must work in all modern browsers.

<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.onmousedown = disable_right_click;
function disable_right_click (e)
     {
          which=(document.all)?event.button:e.which
          if ( which == 2 || which == 3 )
               {
               window.returnValue=false;
               it=true
               return  false ;
               }
          return  true ;
     }
//-->
</SCRIPT>
</HEAD>
<body oncontextmenu="return false"> 
</BODY>
</HTML>

Open in new window


Thanks
Tom
0
Comment
Question by:jhmplondon
  • 3
  • 3
7 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35082110
try

add this to body tag

<body oncontextmenu="return false;">

refer

http://www.experts-exchange.com/expertsZone.jsp
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35082116
I must admit I don't have a script to do that but my only query is to what you're trying to achieve as there is no way to stop someone from copying the images off your site. Disabling javascript will get around your script. Using the keyboard shortcut for the context menu gets around it. Viewing the source and finding the direct URL to the image will get around it as the main page won't be loaded. Taking a screenshot, saving the page locally. etc., all get around this. Put a watermark in your images if they are images for sale. If you don't want them taken from your site, then the only sure fire way I'm afraid is to not put them on the web.

Lee
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35082123
I must admit I don't have a script to do that but my only query is to what you're trying to achieve as there is no way to stop someone from copying the images off your site. Disabling javascript will get around your script. Using the keyboard shortcut for the context menu gets around it. Viewing the source and finding the direct URL to the image will get around it as the main page won't be loaded. Taking a screenshot, saving the page locally. etc., all get around this. Put a watermark in your images if they are images for sale. If you don't want them taken from your site, then the only sure fire way I'm afraid is to not put them on the web.

Lee
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35082135
Blocking the context menu in the body tag is easily circumvented:

http://javascript.about.com/library/blnoright.htm

0
 

Author Comment

by:jhmplondon
ID: 35082284
lsavidge:

I know it's always possible to copy image, in my case I have jQuery modal box open once someone uses right mouse button with code how to embed this image elsewhere (with the link back), however at the moment if someone right click on the image both open - my modal box and context menu, if I disable context menu on the whole page they are then not able to copy the embed code from modal box... That's why I need to disable it only for images.

I script I attached in my first post works fine - so I now how to block it on whole page - but as mentioned above - I need to block it only over images.

This thread exactly shows what I need - however it doesn't work in Safari:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_24833830.html?sfQueryTermInfo=1+10+30+click+disabl+imag+right

So I'd really like to keep the one I have attached in my first post, but add the function to block it only for images.
0
 

Accepted Solution

by:
jhmplondon earned 0 total points
ID: 35082907
Ok, I got it:

 <script type='text/javascript'>
  
  jQuery(function(){
  jQuery('img').bind('contextmenu', function(e){
    return false;
}); 
  });
  
  </script>

Open in new window

0
 

Author Closing Comment

by:jhmplondon
ID: 35126270
it works
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to refer to value 8 62
how can i get the sole odd and th sole even 2 33
jQuery Dialog Autoresize Bug 2 24
binding event to form in window.open 9 35
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question