What this article brings to you is about the reason why the front-end js prohibits the right mouse button and F12 prohibits viewing the source code? (Source code), it has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
HTML In front-end development, when F12 inspects elements, everyone can randomly change the code of part of the page, inject malicious JS, etc. This situation cannot be avoided. Difficult. Although you can still see part of the H5 source code, it cannot be modified.
1. Block F12 review elements
<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>
2. Block right-click menu
<script> document.oncontextmenu = function (event) { if (window.event) { event = window.event; } try { var the = event.srcElement; if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) { return false; } return true; } catch (e) { return false; } }
3. Block paste
<script> document.onpaste = function (event) { if (window.event) { event = window.event; } try { var the = event.srcElement; if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) { return false; } return true; } catch (e) { return false; } } </script>
4. Block copy
<script> document.oncopy = function (event) { if (window.event) { event = window.event; } try { var the = event.srcElement; if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) { return false; } return true; } catch (e) { return false; } } </script>
5. Shielding and cutting
<script> document.oncut = function (event) { if (window.event) { event = window.event; } try { var the = event.srcElement; if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) { return false; } return true; } catch (e) { return false; } }
6. Shielding and selecting
<script> document.onselectstart = function (event) { if (window.event) { event = window.event; } try { var the = event.srcElement; if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) { return false; } return true; } catch (e) { return false; } } </script>
The above is what is the purpose of disabling the right mouse button on the front-end js and prohibiting F12 from viewing the source code? (Source code) full introduction, if you want to know more about JavaScript video tutorial, please pay attention to the PHP Chinese website.
The above is the detailed content of What is the reason why the front-end js prohibits the right mouse button and F12 to prohibit viewing the source code? (source code). For more information, please follow other related articles on the PHP Chinese website!