Thursday, July 18, 2013

Disable right click in a page using javascript


When we want to restrict the right click of the mouse in some of the pages, need to add the below code in the <head> </head> tag within the JAVASCRIPT.

<script type="text/javascript">
var message="Sorry, Right Click has been disabled";
        function clickIE() {if (document.all) {window.console.log(message);return false;}}
       
       function clickNS(e) {
if (document.layers||(document.getElementById&&!document.all)) {
       if (e.which==2||e.which==3) {window.console.log(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>


No comments: