- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
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.
- Javascript: Pop-up window from a HTML link
- Javascript: How to create a simple slide show with clickable links
- Javascript: Pop up Message Box On Submit
- Javascript: Controlling the number of characters entered into a HTML form field.
- Javascript: How to refresh parent window from a pop-up after closing pop-up
- Javascript: Rotate a different image when the page is reloaded
- Javascript: Drop Down Menu List
- Javascript: A simple calendar with today's date highlighted.
- Javascript: How to hide and show other form fields with a select box with Javascript and HTML