How to disable right click on any HTML or PHP webpage.

You may want to protect your webpages from visitors viewing the source code using “right click – view source”. There is a simple bit of script that you can paste on any HTML or PHP page that will disable the right click ability. I use this to protect many of my webpages that have sensitive data within the source code. It is not 100% reliable but will block most visitors from accessing the source code. Obviously some web developers will have their workarounds, however, I have not had a problem so far with this code from dynamicdrive.com.

At dynamicdrive.com you will find all kinds of tools that you can use for free. I hope that you pay their website a visit because they deserve all the credit for helping many web developers with coding and they do it for free.

Ok now lets disable right clicking on your webpages using the simple code below. Just paste it at the bottom of your HTML or PHP page.

<script language=JavaScript>
<!--

//Disable right click script III- By Renigade ([email protected])
//For full source code, visit http://www.dynamicdrive.com

var message="";
///////////////////////////////////
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")
// --> 
</script>

 

Where it says “var message=””; you can add a message that will appear when your website visitors right click the page. This message function will only work in some older browsers and IE.

Also note that most modern browsers, such as Chrome, have a “view source” function built into the menu bar under the “Developer” section as seen below:

View Webpage Source Chrome

It is not perfect but can be used to hide your source code from the normal website visitor with little knowledge of how things work in the developer world.