Friday, November 2, 2012

SharePoint disable image right-click

I was asked to disable right-click on images rendered on SharePoint so users would not be able to download and draw mustaches on people's faces.  After discussing how images really can't be secured, it was still the wish that I disable right-click for images.  The first solution I implemented disabled right-click altogether based on this advice:  http://social.msdn.microsoft.com/Forums/en/sharepointcustomizationlegacy/thread/18414dc7-4319-476d-8257-9c498c5092b9 Basically, I created a content editor web part and put the following code in:

<![CDATA[<html>
<body oncontextmenu= "return false;">
Right Click not allowed on this page
</body>
</html>]]>

Since that worked out, I decided to go a bit further and try to get some javascript to work.  That involved pasting in the following code I saw here http://www.dynamicdrive.com/dynamicindex9/noright2.htm in the content tag.


<![CDATA[<html>
<!-- <body oncontextmenu= "return false;"> -->
<script language="JavaScript1.2">

/*
Disable right click script II (on images)- By Dynamicdrive.com
For full source, Terms of service, and 100s DTHML scripts
Visit
http://www.dynamicdrive.com
*/

var clickmessage="Right click disabled on images!"
function disableclick(e) {
if (document.all) {
if (event.button==2||event.button==3) {
if (event.srcElement.tagName=="IMG"){
alert(clickmessage);
return false;
}
}
}
else if (document.layers) {
if (e.which == 3) {
alert(clickmessage);
return false;
}
}
else if (document.getElementById){
if (e.which==3&&e.target.tagName=="IMG"){
alert(clickmessage)
return false
}
}
}

function associateimages(){
for(i=0;i<document.images.length;i++)
document.images[i].onmousedown=disableclick;
}

if (document.all)
document.onmousedown=disableclick
else if (document.getElementById)
document.onmouseup=disableclick
else if (document.layers)
associateimages()
</script>
</body>
</html>

]]>



No comments:

Post a Comment