web_sites:disabling_right-clicking_by_using_javascript
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
web_sites:disabling_right-clicking_by_using_javascript [2020/05/05 22:12] – peter | web_sites:disabling_right-clicking_by_using_javascript [2020/05/05 22:48] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Web Sites - Disabling right-clicking by using JavaScript ====== | ||
- | |||
- | Simply add the following JavaScript code to the < | ||
- | |||
- | <code javascript> | ||
- | <script language=" | ||
- | /** | ||
- | * Disable right-click of mouse, F12 key, and save key combinations on page | ||
- | */ | ||
- | |||
- | window.onload = function() | ||
- | { | ||
- | document.addEventListener(" | ||
- | e.preventDefault(); | ||
- | }, false); | ||
- | document.addEventListener(" | ||
- | // | ||
- | // " | ||
- | if (e.ctrlKey && e.shiftKey && e.keyCode == 73) { | ||
- | disabledEvent(e); | ||
- | } | ||
- | // " | ||
- | if (e.ctrlKey && e.shiftKey && e.keyCode == 74) { | ||
- | disabledEvent(e); | ||
- | } | ||
- | // " | ||
- | if (e.keyCode == 83 && (navigator.platform.match(" | ||
- | disabledEvent(e); | ||
- | } | ||
- | // " | ||
- | if (e.ctrlKey && e.keyCode == 85) { | ||
- | disabledEvent(e); | ||
- | } | ||
- | // " | ||
- | if (event.keyCode == 123) { | ||
- | disabledEvent(e); | ||
- | } | ||
- | }, false); | ||
- | function disabledEvent(e){ | ||
- | if (e.stopPropagation){ | ||
- | e.stopPropagation(); | ||
- | } else if (window.event){ | ||
- | window.event.cancelBubble = true; | ||
- | } | ||
- | e.preventDefault(); | ||
- | return false; | ||
- | } | ||
- | }; | ||
- | </ | ||
- | </ | ||
- | |||
- | This code will block the right click of mouse, Ctrl + Shift + I, Ctrl+ Shift + J, Ctrl + S, Ctrl + U, and F12 key. | ||
- | |||
- | The F12 key uses for looking the source code of page, so it also need be disabled. | ||
- | |||
- | Also, for addition, you can add the **oncontextmenu** attribute to the **< | ||
- | |||
- | <code html> | ||
- | <body oncontextmenu=" | ||
- | </ | ||
web_sites/disabling_right-clicking_by_using_javascript.1588716751.txt.gz · Last modified: 2020/07/15 09:30 (external edit)