Skip to content

Instantly share code, notes, and snippets.

@Dhruvacube
Created November 19, 2020 06:48
Show Gist options
  • Save Dhruvacube/fa157b3d8a8f3576a80afa4bd5a7bfc2 to your computer and use it in GitHub Desktop.
Save Dhruvacube/fa157b3d8a8f3576a80afa4bd5a7bfc2 to your computer and use it in GitHub Desktop.
JS Code to disable the right click in the webpage!
/**
* Disable right-click of mouse, F12 key, and save key combinations on page
*/
/* You can put this in a js file for directly put it in html flie using script tag */
window.onload = function() {
document.addEventListener("contextmenu", function(e) {
e.preventDefault();
}, false);
document.addEventListener("keydown", function(e) {
//document.onkeydown = function(e) {
// "I" key
if (e.ctrlKey && e.shiftKey && e.keyCode == 73) {
disabledEvent(e);
}
// "J" key
if (e.ctrlKey && e.shiftKey && e.keyCode == 74) {
disabledEvent(e);
}
// "S" key + macOS
if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
disabledEvent(e);
}
// "U" key
if (e.ctrlKey && e.keyCode == 85) {
disabledEvent(e);
}
// "F12" key
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;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment