`
coderliang2018
  • 浏览: 26893 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

javascript 屏蔽按键和禁止地址栏复制

阅读更多
最近有客户反映系统一个页面可以打开过个,因为页面里面有计时功能,会导致计时不准确,网上收集了一些js屏蔽的代码,在这里记录下。
<script type="text/javascript">           
     function contextMenuCheck() {
    	event.returnValue = false;
     }   

     function helpCheck() {
    	return false;
     }      

	//禁止鼠标左键和Ctrl键打开新窗口
	function leftclick() { 
		if (window.event.button == 1 && window.event.ctrlKey) {//2为右键 
		  	alert("禁止操作!");
		} 
	}

    //屏蔽其他功能键   
    function checkKey() {      
		var k = window.event.keyCode;
		//屏蔽 F5 刷新键
	    if (k  ==  116) {                                             
			window.event.keyCode  =  0;      
	        window.event.returnValue =  false;      
	    }      
	    if (window.event.ctrlKey && k == 82)//Ctrl + R      
			window.event.returnValue=   false;      
	    if (window.event.ctrlKey && k == 78)//屏蔽Ctrl+n      
	        window.event.returnValue =  false;      
	    if (window.event.ctrlKey && k == 87)//屏蔽Ctrl+w      
	        window.event.returnValue =  false;      
	    if (event.shiftKey && k==121)//屏蔽 shift+F10      
	        window.event.returnValue = false;      
	    //屏蔽shift+鼠标左键打开新页面
	    if (window.event.srcElement.tagName == "A" && window.event.shiftKey) {
	        //禁止事件冒泡
  		    window.event.cancelBubble = true;
  			  //设置事件的返回值
            window.event.returnValue = false;
            return false;
        }     	      
    }   

	var mouseCur = 0;

	function mouseMove(ev) { 
		ev= ev || window.event; 
		var mousePos = mouseCoords(ev); 
		document.getElementById("yyy").value = mousePos.y; 
		//鼠标y轴的坐标
	    mouseCur = mousePos.y;
	} 

	function mouseCoords(ev) { 
		if(ev.pageX || ev.pageY){ 
			return {x:ev.pageX, y:ev.pageY}; 
		} 
		return { 
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, 
			y:ev.clientY + document.body.scrollTop - document.body.clientTop 
		}; 
	} 

	var winWidth = 0;
	var winHeight = 0;
	//函数:获取尺寸
	function findDimensions() {
		//获取窗口宽度
		if(window.innerWidth)
			winWidth = window.innerWidth;
		else if((document.body) && (document.body.clientWidth))
			winWidth = document.body.clientWidth;
		//获取窗口高度
		if(window.innerHeight)
			winHeight = window.innerHeight;
		else if((document.body) && (document.body.clientHeight))
			winHeight = document.body.clientHeight;
		/*nasty hack to deal with doctype swith in IE*/
		//通过深入Document内部对body进行检测,获取窗口大小
		if(document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
			winHeight = document.documentElement.clientHeight;
			winWidth = document.documentElement.clientWidth;
		}
	}

	window.onblur = function (e) {
		e = e || window.event;
	    if (window.ActiveXObject && /MSIE/.test(navigator.userAgent)) {  //IE
	        //如果 blur 事件是窗口内部的点击所产生,返回 false, 也就是说这是一个假的 blur
	        var x = e.clientX;
	        var y = e.clientY;
	        var w = document.body.clientWidth;
	        var h = document.body.clientHeight;
	
	        if (x >= 0 && x <= w && y >= 0 && y <= h) {
	            window.focus();
	            return false;
	        }
	    }
		//获取鼠标位置
    	findDimensions();
		//如果失去焦点, 并且焦点不在document里面, 在工具栏或者其他窗口
        if (!document.hasFocus() && mouseCur < winHeight) {
			window.focus();
        }
	} 	

	//注册键盘按键
	document.onkeydown = checkKey;
	//注册点击事件
  	document.onclick = checkKey;
	//注册鼠标左键和Ctrl键打开新窗口事件
  	document.onmousedown = leftclick;
	//注册鼠标移动事件
	document.onmousemove = mouseMove; 
	//注册屏蔽鼠标右键      
    document.oncontextmenu = contextMenuCheck;
    //注册屏蔽F1帮助      
    window.onhelp = helpCheck;

</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title>考试窗口</title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

<body>
	<h1>
		考试窗口
	</h1>
	<input id="yyy" type="text">
	鼠标y坐标
</body>
</html>

上面的禁止地址栏复制用到了window.foucs 会影响一直强制在当前窗口,当切换别的窗口的时候有时候会切换不过去, 不知道有其他屏蔽地址栏复制的办法没有。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics