Home > Web Front-end > JS Tutorial > What is the reason why the front-end js prohibits the right mouse button and F12 to prohibit viewing the source code? (source code)

What is the reason why the front-end js prohibits the right mouse button and F12 to prohibit viewing the source code? (source code)

云罗郡主
Release: 2018-10-12 17:36:31
forward
4021 people have browsed it

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.

What is the reason why the front-end js prohibits the right mouse button and F12 to prohibit viewing the source code? (source code)

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>
Copy after login

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;
        }
    }
Copy after login

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>
Copy after login

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>
Copy after login

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;
        }
    }
Copy after login

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>
Copy after login

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!

source:2cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template