Link to home
Start Free TrialLog in
Avatar of mundo2
mundo2Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Disable right click for all images on a page but not non-image parts of the page

I have seen the solution in the code snippet below which was posted by "archrajan" (https://www.experts-exchange.com/questions/21302263/Disable-right-click-on-specific-image.html?sfQueryTermInfo=1+click+disabl+imag+right). This works for the 1st image on the page but if there are multiple images on the page all tagged with id=img1 then the script appears to stop after the 1st image.

How can this script be modifed to include all images on the page which contain id=img1 ?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript1.1">
<!-- Begin
function right(e) {
if (navigator.appName == 'Netscape' &&
(e.which == 3 || e.which == 2))
return false;
else if (navigator.appName == 'Microsoft Internet Explorer' &&
(event.button == 2 || event.button == 3)) {
alert("Sorrrrry!!!For security resons, the right click feature has been disabled.");
return false;
}
return true;
}
function init()
{
document.getElementById('img1').onmousedown=right;
if (document.layers) window.captureEvents(Event.MOUSEDOWN);
document.getElementById('img1').onmousedown=right;
}
//  End -->
</SCRIPT>
</HEAD>
 
<BODY onload = "init();">
 this code will show an alert when right click clicked ....
 
 
<img src = "http://us.i1.yimg.com/us.yimg.com/i/us/nt/ma/ma_mail_1.gif" id = "img1">
 
</BODY>
</HTML>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Barry Jones
Barry Jones
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Keep in mind that any of this can be avoid by the user simply turning of javascript, so it really does not secure much.
Agree with naspinski: firefox/firebug allows the user to load anything from the page basically.. :)
BTW, re: "How can this script be modifed to include all images on the page which contain id=img1 ?" - ID's should be unique on the page.  Better to use the "name" attribute or even the "rel" attribute.

Here is the code for a single ID:
$('img#myid').bind("contextmenu",function(e){
  return false;
});

Open in new window

Avatar of mundo2

ASKER

I'm aware that it's only a partial solution and anyone with a bit of knowledge can get round it. Just required for an Intranet project!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mundo2

ASKER

Thanks guys
Glad to help, thanks for the points.