Home > Article > Web Front-end > Two JS methods to shield the right mouse button_javascript skills
Two ways to block the right mouse button in JS, a relatively common small JS function. After using this code, visitors will not be able to right-click when visiting your webpage, and the right-click menu will not pop up. This It is best not to use too many functions, as sometimes users will be disgusted.
The code is as follows:
<html> <head> <title>屏蔽鼠标右键</title> </head> <body oncontextmenu=self.event.returnvalue=false> 第一种方法:在body标签里加上oncontextmenu=self.event.returnvalue=false; <br> <script language="javascript"> <!-- function document.oncontextmenu() { return false; } function nocontextmenu() { if(document.all) { event.cancelBubble=true; event.returnvalue=false; return false; } } --> </script> 第二种方法:在body里加入onmousedown="rclick()" oncontextmenu= "nocontextmenu()" <br> <script language="javascript"> <!-- function rclick() { if(document.all) { if (event.button == 2){ event.returnvalue=false; } } } --> </script> <br>详细情况请查看代码。<br> <br>现在点击你的鼠标右键,不起作用了。 </body> </html>
The above is the entire content of this article, I hope it will be helpful to everyone’s study.