Javascript: Disabling right click on a web page

Want to protect your source code?

Using this code will prevent the vast majority of users from right-clicking over a page and choosing "View source", or right-clicking over an image and saving it. There are other ways to view the code, though... This is not always a wise technique to use - it would have to be a very special piece of code to want to hide, and those who would want to steal it will always be able to find a way to see it.

1. Disable right click script

<SCRIPT TYPE="text/javascript">
<!--
//Disable right click script
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if
(document.layers(document.getElementById&&!document.all)) {
if (e.which==2e.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>



2. To disable the IE imagetoolbar also add the META TAG below to the HEAD

<META HTTP-EQUIV="imagetoolbar" CONTENT="no">


3. To protect TEXT add 'Disable Text Select' code below into of your HTML.


<SCRIPT TYPE="text/javascript">
<!--
//Disable select-text script (IE4+, NS6+)
function disableselect(e){
return false
}
function reEnable(){
return true
}
//if IE4+
document.onselectstart=new Function ("return false")
//if NS6
if (window.sidebar){
document.onmousedown=disableselect
document.onclick=reEnable
}
// -->
</SCRIPT>


Enjoy!

Before you leave:
Do you find this article useful? Any suggestion, question or comment? Please post it in the comments below.

Read Users' Comments (0)

0 Response to "Javascript: Disabling right click on a web page"

Post a Comment