JS如何禁止别人查看网站源码

四种查看路径:

查看效果:猛戳

1、直接按F12

2、Ctrl+Shift+I查看

3、鼠标点击右键查看

4、Ctrl+u=view-source:+url

把以上三种状态都屏蔽掉就可以了,document有onkeydown(键盘按键事件),该事件里面找到对应的keycode并处理就可以,

document也有oncontextmenu鼠标右键事件,屏蔽即可。4里面的Ctrl+u是可以屏蔽的,【但是如果你在URL前面加上view-source:后刷新还是可以看的^_^】

JS撸码如下:

window.onload=function(){
        document.onkeydown=function(){
            var e=window.event||arguments[0];
            if(e.keyCode==123){
                alert("小样你想干嘛?");
                return false;
            }else if((e.ctrlKey)&&(e.shiftKey)&&(e.keyCode==73)){
                alert("还是不给你看。。");
                return false;
            }else if((e.ctrlKey)&&(e.keyCode==85)){//追加

        return false;
       }
        };
        document.oncontextmenu=function(){
            alert("小样不给你看");
            return false;
        }
    }
原文地址:https://www.cnblogs.com/-walker/p/5952442.html