前端js禁止鼠标右键及F12禁止查看源代码

更多请关注微信公众号---my搬运工

超低 特价云服务器 60元 /年👈

一、屏蔽F12 审查元素

<script>
document.onkeydown = function () {
if (window.event && window.event.keyCode == 123) {
alert(“F12被禁用”);
event.keyCode = 0;
event.returnValue = false;
}
if (window.event && window.event.keyCode == 13) {
window.event.keyCode = 505;
}
if (window.event && window.event.keyCode == 8) {
alert(str + “\n请使用Del键进行字符的删除操作!”);
window.event.returnValue = false;
}
}
</script>

 

二、屏蔽右键菜单

  1. <script>
  2. document.oncontextmenu = function (event) {
  3. if (window.event) {
  4. event = window.event;
  5. }
  6. try {
  7. var the = event.srcElement;
  8. if (!((the.tagName == “INPUT” && the.type.toLowerCase() == “text”) || the.tagName == “TEXTAREA”)) {
  9. return false;
  10. }
  11. return true;
  12. } catch (e) {
  13. return false;
  14. }
  15. }

 

 

三、屏蔽粘贴

  1. <script>
  2. document.onpaste = function (event) {
  3. if (window.event) {
  4. event = window.event;
  5. }
  6. try {
  7. var the = event.srcElement;
  8. if (!((the.tagName == “INPUT” && the.type.toLowerCase() == “text”) || the.tagName == “TEXTAREA”)) {
  9. return false;
  10. }
  11. return true;
  12. } catch (e) {
  13. return false;
  14. }
  15. }
  16. </script>

四、屏蔽复制

  1. <script>
  2. document.oncopy = function (event) {
  3. if (window.event) {
  4. event = window.event;
  5. }
  6. try {
  7. var the = event.srcElement;
  8. if (!((the.tagName == “INPUT” && the.type.toLowerCase() == “text”) || the.tagName == “TEXTAREA”)) {
  9. return false;
  10. }
  11. return true;
  12. } catch (e) {
  13. return false;
  14. }
  15. }
  16. </script>

五、屏蔽剪切

  1. <script>
  2. document.oncut = function (event) {
  3. if (window.event) {
  4. event = window.event;
  5. }
  6. try {
  7. var the = event.srcElement;
  8. if (!((the.tagName == “INPUT” && the.type.toLowerCase() == “text”) || the.tagName == “TEXTAREA”)) {
  9. return false;
  10. }
  11. return true;
  12. } catch (e) {
  13. return false;
  14. }
  15. }

 

六、屏蔽选中

  1. <script>
  2. document.onselectstart = function (event) {
  3. if (window.event) {
  4. event = window.event;
  5. }
  6. try {
  7. var the = event.srcElement;
  8. if (!((the.tagName == “INPUT” && the.type.toLowerCase() == “text”) || the.tagName == “TEXTAREA”)) {
  9. return false;
  10. }
  11. return true;
  12. } catch (e) {
  13. return false;
  14. }
  15. }
  16. </script>

(本文转自博客园的 Clark_Kent ,感谢经验分享!)

 

 

测试网站:tk.hssd.xyz

路遥知马力,日久见人心
言如玉 » 前端js禁止鼠标右键及F12禁止查看源代码