日期:2014-05-16  浏览次数:20428 次

递归 添加 JS 监听 (IE)
废话不多说直接上代码
想直接用

没问题!直接复制到项目里,随你放在哪一层Frame里,那一层所包含的所有Frame--无论是已经加载的,还是需要用户响应才会加载的,只要加载完毕,就给他加上个监听。

我的这段代码只是为了禁用Ctrl+N 键的,所以只用了onkeydown事件,
当然你可以改成其他事件,也可以禁用其他键盘操作。
当然还有缺陷拉,请各位斧正!
只适合IE,没空做兼容
<script language="javascript" type="text/javascript">
	//window.onerror=function(){return false;};//为false时 
</script>	
<script language="javascript" type="text/javascript">
	window.debugflag = true;
	 function keydown(){
 		try{
			if(arguments[0]&&(arguments[0].ctrlKey)&&(arguments[0].keyCode=='78')){
					arguments[0].returnValue=false;
					var d =arguments[0];
					alert('Reject this action! \n\nDocTitle:'+arguments[0].srcElement.document.title);
				}
		}catch(e){
			if(window.debugflag)alert(e.message);
		 }
		}
      document.onreadystatechange = function(){
            if(document.readyState=="complete"){
					addKeyDownListener(window.document);
			}
		}
      function frameonready(){
    	  try {
    	  if(arguments[0]&&arguments[0].srcElement&&arguments[0].srcElement.readyState=="complete"){
				var d = arguments[0].srcElement;
           d.contentWindow.document.attachEvent("onkeydown",keydown);
	      	}
	      } catch (e) {
				if (window.debugflag)alert(e.message);
			}
      }	
      function frameonload(){
    	  try {
        	  alert("onload---\n\nDocTitle:"+arguments[0].srcElement.document.title);
    	 	 if(arguments[0].srcElement.readyState=="complete"){
				var d = arguments[0].srcElement;
               d.document.attachEvent("onkeydown",keydown);
	           if(d.contentWindow!=d.contentWindow.parent){
	           	   addKeyDownListener(arguments[0].srcElement.document);
	           }
      		}
	      } catch (e) {
				if (window.debugflag)alert(e.message);
			}
      }	
	function propertychange(e) {
		try {
			if(e.propertyName=='src'){
            	alert("src change");
            	arguments[0].srcElement.attachEvent("onreadystatechange",frameonready);
                addKeyDownListener(arguments[0].srcElement.document);
            }
		} catch (e) {
			if (window.debugflag)
				alert(e.message);
		}
	}
	function addKeyDownListener(doc) {
		try{
			var len = 0;
			if (doc.frames) {
				len = doc.frames.length;
			}
			if (len > 0) {
				for ( var i = 0; i < len; i++) {
					//alert("frameName:"+doc.frames[i].name+"-- len:"+doc.frames[i].frames.length);
					if(!doc.frames[i].document.keydownEventCount){
						doc.frames[i].document.attachEvent("onkeydown", keydown);
						doc.frames[i].document.keydownEventCount=1;
					}
					var tmpframe = doc.getElementsByTagName("FRAME")[i];
					if(!tmpframe){//如果内嵌的是iframe tmpframe对象就为null
						tmpframe = doc.getElementsByTagName("IFRAME")[i];
						if(!tmpframe.readyEventCount){//判断tmpframe对象是否已定义readyEventCount属性并赋值,若已定义并赋值了,说明就不需要再添加listener
							tmpframe.attachEvent("onreadystatechange",frameonready);
							tmpframe.readyEventCount=1;
						}
					}else 
					if(!tmpframe.loadEventCount){
						tmpframe.attachEvent("onload",frameonload);
						tmpframe.loadEventCount=1;
					}
					addKeyDownListener(doc.frames[i].document);
				}
			}
		} catch (e) {
			if (window.debugflag)
				alert(e.message);
		}
	}
	if (document.attachEvent) {
		document.attachEvent("onkeydown", keydown);
	} else {
		document.addEventListener("onkeydown", keydown, true);
		alert("Warning");
	}
</script>