Disabling right-clicking by using JavaScript

It makes sense to do everything you can to protect your copyrighted content.

While there is no way to completely stop people from stealing your images, you can make it harder.

Disabling right-clicking is one method you can use to deter casual theft.

Step 1:   Add the oncontextmenu attribute to the <body>  tag of your page HTML code:

<body oncontextmenu="return false;">


Step 2:   Add the following JavaScript code to the <body>  section of your web page:

<script language="JavaScript">
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;
}
};
</script >

This code will block the right click of mouse, Ctrl + Shift + I, Ctrl+ Shift + J, Ctrl + S, Ctrl + U, and F12 key.


  12th-February-2020

  Adarsh Vishwakarma



Leave a Comment



All Rights are Reserved