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

  • How to Install a Ceiling Fan
    8

    When installing a home ceiling fan you want to make sure that your follow several safety tips and warnings so that you prevent fire hazards, electrical shock, and personal injury. Read carefully when looking over the...

  • How to Get Rid of Mice In and Around The House?
    5

    Until this year, the only time that I've had mice or rats in the house were when my daughter had pet mice and rats, and one of those times we did have one loose in the house. (Science fair project got loose.) Anyway,...

  • How to Build Wooden Roof Trusses
    23

    You may refer to a truss as the rafter, but it's basically the skeleton of the roof, carrying the weight of the frame and supporting the walls of the building. Trusses are very important to preventing the walls from...


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