Javascript: How to disable Right Click option

Disabling the right click for Browser

Have you ever used your mouse right click to try to view source of a certain web page. Either to look at javascript source, or maybe the HTML syntax on how something was done on the web page, or the images used on a site. I think most all web developers have used the right click option, and still use it today. This simple Javascript code will disable the right click feature. I've tested it with Microsoft IE and Firefox, and it seems to work well.

Now, users if quick on their toes can still view the source of your page by using the browser menu. However, I think most will not even go there. It's still a nice deterant to include in your pages.

Sample Javascript Code Below. Version 1

Place the javascript code between the <HEAD></HEAD> tags of your web page.

<SCRIPT>
function click() 
{
if (event.button==2) 
    {    
        alert('Warning Message Line 1 here\nWarning Message Line 2 here\nWarning Message Line 3 here\nWarning Message Line 4 here');
    }
}document.onmousedown=click
// - ->
</SCRIPT> 

Sample Javascript Code Below. Version 2

If the first sample does not work for you, please try this version. Place the sample javascript code between the <HEAD></HEAD> tags of your web page.

<SCRIPT LANGUAGE="JavaScript1.1">
<!-- Begin
function right(e) {
var msg = "Right-clicking is not possible in this document.";
if (navigator.appName == 'Netscape' && e.which == 3) {
alert(msg); 
return false;
}
else
if (navigator.appName == 'Microsoft Internet Explorer' &&
event.button==2) {
alert(msg); 
return false;
}
return true;
}
document.onmousedown = right;
// End -->
</SCRIPT>  

Conclusion

remember, users can still view the source of your page by using the browser menu. However, I think most will not even go there. Give it a try.

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working