利用JS脚本禁止浏览器退格、右键、全选、复制、粘贴、Ctrl、Alt、Shift等按键操作

一、禁用浏览器退格键

使用方法:把下面的js代码放到<head></head>之间就ok了

<script type="text/javascript">
        //处理键盘事件 禁止后退键(Backspace)密码或单行、多行文本框除外
        function forbidBackSpace(e) {
            var ev = e || window.event; //获取event对象 
            var obj = ev.target || ev.srcElement; //获取事件源 
            var t = obj.type || obj.getAttribute('type'); //获取事件源类型 
            //获取作为判断条件的事件类型 
            var vReadOnly = obj.readOnly;
            var vDisabled = obj.disabled;
            //处理undefined值情况 
            vReadOnly = (vReadOnly == undefined) ? false : vReadOnly;
            vDisabled = (vDisabled == undefined) ? true : vDisabled;
            //当敲Backspace键时,事件源类型为密码或单行、多行文本的, 
            //并且readOnly属性为true或disabled属性为true的,则退格键失效 
            var flag1 = ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vDisabled == true);
            //当敲Backspace键时,事件源类型非密码或单行、多行文本的,则退格键失效 
            var flag2 = ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea";
            //判断 
            if (flag2 || flag1) return false;
        }
        //禁止后退键 作用于Firefox、Opera
        document.onkeypress = forbidBackSpace;
        //禁止后退键  作用于IE、Chrome
        document.onkeydown = forbidBackSpace;
</script>


二、禁止鼠标右键、全选、复制、粘贴

禁用右键菜单

document.oncontextmenu = function(){
    event.returnValue = false;
}
// 或者直接返回整个事件
document.oncontextmenu = function(){
    return false;
}


禁用网页上选取的内容

document.onselectstart = function(){
    event.returnValue = false;
}
// 或者直接返回整个事件
document.onselectstart = function(){
    return false;
}


oncopy事件禁用复制

document.oncopy = function(){
    event.returnValue = false;
}
// 或者直接返回整个事件
document.oncopy = function(){
    return false;
}


以上三种事件,如果只想单纯的禁用鼠标右键,和复制粘贴,还可以将它们直接写到HTML中的body上面;

<body oncontextmenu = "return false" ></body>

<body onselectstart = "return false" ></body>

<body oncopy = "return false" ></body>


禁用鼠标事件

document.onmousedown = function(e){
    if ( e.which == 2 ){// 鼠标滚轮的按下,滚动不触发
        return false;
    }
    if( e.which==3 ){// 鼠标右键
        return false;
    }
}


禁用键盘中的ctrl、alt、shift

document.onkeydown = function(){
    if( event.ctrlKey ){
        return false;
    }
    if ( event.altKey ){
        return false;
    }
    if ( event.shiftKey ){
        return false;
    }
}


一个更简单的方法就是在<body>中加入如下的代码,这样鼠标的左右键都失效了.

    oncontextmenu='return false'
  ondragstart='return false' 
  onselectstart ='return false' 
  onselect='document.selection.empty()' 
  oncopy='document.selection.empty()' 
  onbeforecopy='return false' 
  onmouseup='document.selection.empty()'


禁止网页另存为

在<body>后面加入以下代码:

<noscript> 
  <iframe src="*.htm"></iframe> 
</noscript>


禁止网页内容复制.粘贴

在<body>中加入以下代码:

<body onmousemove=/HideMenu()/ oncontextmenu="return false" 
ondragstart="return false" onselectstart ="return false" 
onselect="document.selection.empty()" 
oncopy="document.selection.empty()" onbeforecopy="return false" 
onmouseup="document.selection.empty()"></body>


This article comes from http://www.chieng.cn, reproduced please specify!
打赏 支付宝打赏 微信打赏

评论

Top