JS禁止查看网页源代码

JS禁止查看网页源代码
查看源代码的几种方法:

  1. 直接按F12
  2. Ctrl+Shift+I查看
  3. 鼠标点击右键查看
  4. Ctrl+u=view-source:+url

 

以上的几种方法都可以查看到网站的源代码,我们可以通过使用JavaScript来屏蔽掉这三种状态从而实现禁止查看源代码效果。

<script>
        window.onload = function() {
        document.onkeydown = function() {
            var e = window.event || arguments[0];
            //屏蔽F12
            if(e.keyCode == 123) {
                return false;
             //屏蔽Ctrl+Shift+I
            } else if((e.ctrlKey) && (e.shiftKey) && (e.keyCode == 73)) {
                return false;
             //屏蔽Shift+F10
            } else if((e.shiftKey) && (e.keyCode == 121)){
                 return false;
        //屏蔽Ctrl+U
            } else if((e.ctrlKey) && (e.keyCode == 85)){
                     return false;
            }
        };
        //屏蔽右键单击
        document.oncontextmenu = function() {
    alert("右键被禁止,复制内容请按CTRL+C!");
        return false;
        }
    }
</script>

经过JS压缩后的代码如下:

<script>window.onload=function(){document.onkeydown=function(){var e=window.event||arguments[0];if(e.keyCode==123){return false}else{if((e.ctrlKey)&&(e.shiftKey)&&(e.keyCode==73)){return false}else{if((e.shiftKey)&&(e.keyCode==121)){return false}else{if((e.ctrlKey)&&(e.keyCode==85)){return false}}}}};document.oncontextmenu=function(){alert("右键被禁止,复制内容请按CTRL+C!");return false}};</script>

真正能实现源代码的屏蔽,单纯的JS是不可能完全屏蔽的!这些脚本只能简单防止小白,对计算机老鸟、大神是无法作用的。并且现在很多浏览器自带有查看网页源代码的功能。

其他相关

一、屏蔽F12 审查元素

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;
    }

}

除了屏蔽这个,我们还有其他有趣的设置:

二、屏蔽右键菜单

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;
}
}

三、屏蔽粘贴

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;
}
}

四、屏蔽复制

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;
}
}

五、屏蔽剪切

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走内容就不好了

六、屏蔽选中

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;
}
}
© 版权声明
1:本网站名称:源码库
2:本站永久网址:www.ymkuz.com
3:本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
4:分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
5:本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
6:本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7:如有链接无法下载、失效或广告,请联系管理员处理!
8:文章投稿-投诉建议E-mail:yunduanw@qq.com 站长QQ:99767152
THE END
点赞1 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容