JS disable the right key disable Ctrl+u disable Ctrl+s disable the implementation code of F12

  • 2021-08-28 19:00:36
  • OfStack

Let's start with two simple and rude ones

The first commonly used function code


function keycodes(){
 //  Right-click prohibition 
 document.oncontextmenu=function(){return false};
 document.onkeydown = function(e) {
   e = window.event || e;
   var k = e.keyCode;
   // Shielding ctrl+u , F12 Key 
   if ((e.ctrlKey == true && k == 85) || k == 123) {
     e.keyCode = 0;
     e.returnValue = false;
     e.cancelBubble = true;
     return false;
   }
 }
}

Method 2:


var arr = [123, 17, 18];
document.oncontextmenu = new Function("event.returnValue=false;"),// Disable the right key 
document.onselectstart = new Function("event.returnValue=false;"),// Disable selection 
window.onkeydown = function (e) {
 var code = e.keyCode;
 if (arr.indexOf(code) > -1) {
 console.log(" Be recruited ", code);
 e.preventDefault();
 }
}, window.oncontextmenu = function (e) {
 e.preventDefault();
}

Method 3:


var arr = [123, 17, 18];
document.oncontextmenu = new Function("event.returnValue=false;"),// Disable the right key 

window.onkeydown = function (e) {
 var keyCode = e.keyCode || e.which || e.charCode;
 var ctrlKey = e.ctrlKey || e.metaKey;
 console.log(keyCode + "--" + keyCode);
 if (ctrlKey && keyCode == 85) {
 console.log("ctrl+u");
 e.preventDefault();
 }
 if (arr.indexOf(keyCode) > -1) {
 console.log(" Others ");
 e.preventDefault();
 }
 //e.preventDefault();
 //return false;
}

1 Some commonly used codes

1. Realize the prohibition of selecting text.


<script>
// The website prohibits the selection of text 
  document.body.onselectstart = function() {
  self.event.returnValue=false
};
</script>

2. Realize the prohibition of the right mouse button.


<script>
  // Right-click is prohibited on the website 
  document.body.oncontextmenu = function() {
  self.event.returnValue=false
};
</script>

3. The implementation prohibits F12 from reviewing elements.


<script>
	// Key trigger 
document.onkeydown = function(){
  // Prohibit  F12
  if (window.event && window.event.keyCode == 123) {
  event.keyCode = 0;
  event.returnValue = false;
  }
  }
</script>

4. Realize the prohibition of Ctrl + U to view the source code.


<script>
	// Key trigger 
document.onkeydown = function(){
  // Prohibit ctrl+u
  if (event.ctrlKey && window.event.keyCode==85){
  return false;
  }
  }
</script>

5. Realize the prohibition of saving Ctrl+S web pages as.


<script>
	// Key trigger 
document.onkeydown = function(){
	// Prohibit ctrl+s
  if (event.ctrlKey && window.event.keyCode==83){
  return false;
  }
  }
</script>

6. Realize that F5 is prohibited from refreshing the page.


<script>
	// Key trigger 
document.onkeydown = function(){
	// Prohibit  F5 
  if (window.event && window.event.keyCode == 116) {
  event.keyCode = 0;
  event.returnValue = false;
  }
  }
</script>

all, if you want to achieve the above effects at the same time, as follows:


<script>
	// Key trigger 
document.onkeydown = function(){
  // Prohibit ctrl+u
  if (event.ctrlKey && window.event.keyCode==85){
  return false;
  }
  // Prohibit  F12
  if (window.event && window.event.keyCode == 123) {
  event.keyCode = 0;
  event.returnValue = false;
  }
	// Prohibit ctrl+s
  if (event.ctrlKey && window.event.keyCode==83){
  return false;
  }
	// Prohibit  F5 
  if (window.event && window.event.keyCode == 116) {
  event.keyCode = 0;
  event.returnValue = false;
  }
  }
  // Right-click is prohibited on the website 
  document.body.oncontextmenu = function() {
  self.event.returnValue=false
};
// The website prohibits the selection of text 
  document.body.onselectstart = function() {
  self.event.returnValue=false
};
</script>

JavaScript shields most buttons such as right key F12 and Ctrl+U


var arr = [123, 17, 18];
document.oncontextmenu = new Function("event.returnValue=false;"),// Disable the right key 
document.onselectstart = new Function("event.returnValue=false;"),// Disable selection 
window.onkeydown = function (e) {
 var code = e.keyCode;
 if (arr.indexOf(code) > -1) {
 console.log(" Be recruited ", code);
 e.preventDefault();
 }
}, window.oncontextmenu = function (e) {
 e.preventDefault();
}
0

Behavior codes related to prohibition of right-click on web pages: prohibition of copying, prohibition of F12, prohibition of ctrl+u, prohibition of right-click on websites

Prevent the website article to be reproduced 1 some conventional methods, prohibit copying, prohibit F12, prohibit ctrl+u, the website prohibits right-click; But these are just a layer of protection, should be ordinary novice editors enough, if others really want to copy your site content, or there are other ways to copy and paste.

Here's the code:


var arr = [123, 17, 18];
document.oncontextmenu = new Function("event.returnValue=false;"),// Disable the right key 
document.onselectstart = new Function("event.returnValue=false;"),// Disable selection 
window.onkeydown = function (e) {
 var code = e.keyCode;
 if (arr.indexOf(code) > -1) {
 console.log(" Be recruited ", code);
 e.preventDefault();
 }
}, window.oncontextmenu = function (e) {
 e.preventDefault();
}
1

The above code is the selected page text. When you press ctrl+c to copy, a prompt box will pop up to prevent the copy operation.

Among them,

document. location. href is the current page url;

\ u539f\ u6587\ uff1a Chinese characters encoded by Unicode are transcoded as "original:";

\ u53d7\ u539f\ u521b\ u8bae\ u4d4\ uff0c\ u6d4f\ u89c8\ u5668\ u6682\ u4d\ u652f\ u6301\ u590ES112 EN\ u5236\ u7c98\ u8d34 is also a string encoded by Unicode, which means: protected by the original protocol, the browser does not support copy and paste for the time being;

The coding content of Unicode can be modified with the help of one tool. Here is the link of the webmaster tool Unicode coding conversion: http://tool.chinaz.com/tools/unicode.aspx


var arr = [123, 17, 18];
document.oncontextmenu = new Function("event.returnValue=false;"),// Disable the right key 
document.onselectstart = new Function("event.returnValue=false;"),// Disable selection 
window.onkeydown = function (e) {
 var code = e.keyCode;
 if (arr.indexOf(code) > -1) {
 console.log(" Be recruited ", code);
 e.preventDefault();
 }
}, window.oncontextmenu = function (e) {
 e.preventDefault();
}
2

The above code is the prompt triggered when pressing the relevant key or pressing the right mouse button.

How to Disable the Right Mouse Button and F12, Keyboard Save As, Paste, Copy Events on Web Pages

Recently, the commercial background project involves front-end encryption technology. In order to ensure that the data displayed on the front-end page is not stolen, some treatments have been done on js. These treatments can not completely prevent people with heart from stealing data, but only increase the difficulty of their operation.

1. Disable browser right-click events. You can save the web page by right-clicking the browser. Or review the source code of the web page, This is something we want to avoid (of course, You can still open the developer tool through the toolbar in the upper right corner of the browser, where you can judge the cursor position of the mouse. If the cursor moves to the navigation bar and the place outside the page body, we can choose to empty the elements displayed on the page, so that the source code seen in the console has no data displayed).

js implementation:


<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title> Disable the right mouse button event </title>
</head>
<body>
<p> This is 1 Some content , Show the right mouse button disabled event 
</p>
</body>
<script>
 document.oncontextmenu = function(){
 return false;
 }
</script>
</html>

jquery implementation:


var arr = [123, 17, 18];
document.oncontextmenu = new Function("event.returnValue=false;"),// Disable the right key 
document.onselectstart = new Function("event.returnValue=false;"),// Disable selection 
window.onkeydown = function (e) {
 var code = e.keyCode;
 if (arr.indexOf(code) > -1) {
 console.log(" Be recruited ", code);
 e.preventDefault();
 }
}, window.oncontextmenu = function (e) {
 e.preventDefault();
}
4

2. Disable F12 event, keyboard select paste copy event

jquery implementation:


var arr = [123, 17, 18];
document.oncontextmenu = new Function("event.returnValue=false;"),// Disable the right key 
document.onselectstart = new Function("event.returnValue=false;"),// Disable selection 
window.onkeydown = function (e) {
 var code = e.keyCode;
 if (arr.indexOf(code) > -1) {
 console.log(" Be recruited ", code);
 e.preventDefault();
 }
}, window.oncontextmenu = function (e) {
 e.preventDefault();
}
5

js implementation:


var arr = [123, 17, 18];
document.oncontextmenu = new Function("event.returnValue=false;"),// Disable the right key 
document.onselectstart = new Function("event.returnValue=false;"),// Disable selection 
window.onkeydown = function (e) {
 var code = e.keyCode;
 if (arr.indexOf(code) > -1) {
 console.log(" Be recruited ", code);
 e.preventDefault();
 }
}, window.oncontextmenu = function (e) {
 e.preventDefault();
}
6

Automatic page jump after js masking chrome F12


var arr = [123, 17, 18];
document.oncontextmenu = new Function("event.returnValue=false;"),// Disable the right key 
document.onselectstart = new Function("event.returnValue=false;"),// Disable selection 
window.onkeydown = function (e) {
 var code = e.keyCode;
 if (arr.indexOf(code) > -1) {
 console.log(" Be recruited ", code);
 e.preventDefault();
 }
}, window.oncontextmenu = function (e) {
 e.preventDefault();
}
7

Specifically, s () is executed after the page is loaded; Determine if there is a debugging tool
And press f12 to trigger window. onresize judgment, and press key judgment


Related articles: