JS禁止右键|禁止选中|禁止ctrl+u|禁止F12代码

文章最后更新时间:2025-09-03 00:19:05

图片[1]-JS禁止右键|禁止选中|禁止ctrl+u|禁止F12代码-九二分享

基础防复制方式:

 过js 禁止鼠标右键、禁止鼠标选中文字、禁止快捷键ctrl+u、禁止F12键

第一种方式

var arr = [123, 17, 18];
document.oncontextmenu = new Function("event.returnValue=false;");//禁用右键
document.onselectstart = new Function("event.returnValue=false;");//禁用选中
//禁止 ctrl+u
window.onkeydown = function (e) {
    var keyCode = e.keyCode || e.which || e.charCode;
    var ctrlKey = e.ctrlKey || e.metaKey;
    console.log(keyCode + "--" + keyCode);
    if (ctrlKey && keyCode == 85) {
	console.log("禁止ctrl+u");
	e.preventDefault();
    }
    if (arr.indexOf(keyCode) > -1) {
	console.log("其他");
	e.preventDefault();
    }
}
//屏蔽F12
document.onkeydown = function () {
    if (window.event && window.event.keyCode == 123) {
	event.keyCode = 0;
	event.returnValue = false;
	return false;
    }
}

第二种方式

<script>
	//按键触发
document.onkeydown = function(){
    //禁止ctrl+u
    if (event.ctrlKey && window.event.keyCode==85){
    return false;
    }
    //禁止 F12
    if (window.event && window.event.keyCode == 123) {
    event.keyCode = 0;
    event.returnValue = false;
    }
	//禁止ctrl+s
    if (event.ctrlKey && window.event.keyCode==83){
    return false;
    }
	//禁止 F5 
    if (window.event && window.event.keyCode == 116) {
    event.keyCode = 0;
    event.returnValue = false;
    }
    }
    //网站禁止右键
    document.body.oncontextmenu = function() {
    self.event.returnValue=false
};
//网站禁止选择文字
    document.body.onselectstart = function() {
    self.event.returnValue=false
};
</script>

第三种方式

<script>
    //禁用F12
    window.onkeydown = window.onkeyup = window.onkeypress = function (event) {
        // 判断是否按下F12,F12键码为123
        if (event.keyCode == 123) {
            event.preventDefault(); // 阻止默认事件行为
            window.event.returnValue = false;
        }
    }
    //禁用调试工具
    var threshold = 160; // 打开控制台的宽或高阈值
    // 每秒检查一次
    var check = setInterval(function() {
        if (window.outerWidth - window.innerWidth > threshold ||
            window.outerHeight - window.innerHeight > threshold) {
            // 如果打开控制台,则刷新页面
            // window.location.reload();
        }
    }, 1000)
    //屏蔽右键菜单
    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.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;
        }
    }
    //屏蔽复制
    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;
        }
    }
    //屏蔽粘贴
    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;
        }
    }
    window.onload=function(){
        document.onkeydown=function(){
            var e = window.event||arguments[0];
            if(e.keyCode==123){
                //F12
                showMsg();
                return false;
            }else if((e.ctrlKey)&&(e.shiftKey)&&(e.keyCode==73)){
                //ctrl + shift + i
                showMsg();
                return false;
            }else if((e.ctrlKey)&&(e.keyCode==85)){
                //ctrl + U
                showMsg();
                return false;
            }else if((e.ctrlKey)&&(e.keyCode==83)){
                //ctrl + U
                showMsg();
                return false;
            }
        }
        document.oncontextmenu=function(){
            //右击
            showMsg();
            return false;
        }
    }

    function showMsg() {
        $(function () {
            //弹窗调用接口
            function tc(txt) {
                var html = "<div class=\"box_fixed\"><div class=\"box_fixed_box\"><h4>温馨提示</h4><span></span><div>我知道了</div></div></div>";
                $("body").append(html);
                $(".box_fixed_box span").text(txt);
                $(".box_fixed").show();
                $(".box_fixed_box div").click(function () {
                    $(".box_fixed").remove();
                });
            };
            tc("很抱歉,CV大法被禁止!为保护作者权益,本博客文章不允许复制。\n\n如有特殊需要,请添加作者微信");
        });

}
</script>

 

 
© 版权声明
THE END
喜欢就支持一下吧
点赞0
评论 抢沙发

请登录后发表评论

    暂无评论内容