前端杂谈 · Web

JavaScript禁用F12和鼠标右键

小编 · 11月1日 · 2019年 ·

代码加密是一项很头疼的事情,在这记录下一种自我安慰型的“代码加密”方式

JavaScript禁用F12和鼠标右键

代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body{
            width: 100%;
            height: 100%;
            background: #c3c3c3;
        }
        h3{
            margin-top: 300px;
            font-size: 60px;
            text-align: center;
        }
    </style>
</head>
<body>
    <h3>禁止鼠标右键菜单和F12打开控制台看源码</h3>
    <script type="text/javascript">
        //禁止鼠标右键菜单和F12打开控制台看源码
        function click(e) {
        if (document.all) {
        if (event.button==2||event.button==3) {
        // alert("欢迎光临寒舍,有什么需要帮忙的话,请与站长联系!谢谢您的合作!!!");
        oncontextmenu='return false';
        }
        }
        if (document.layers) {
        if (e.which == 3) {
        oncontextmenu='return false';
        }
        }
        }
        if (document.layers) {
        document.captureEvents(Event.MOUSEDOWN);
        }
        document.onmousedown=click;
        document.oncontextmenu = new Function("return false;")
        document.onkeydown =document.onkeyup = document.onkeypress=function(){
        if(window.event.keyCode == 123) {
        window.event.returnValue=false;
        return(false);
        }
        }
        // <--123——112是F1-F12的代码数-->
    </script>
</body>
</html>
0 条回应