递归 添加 JS 监听 (IE)

本文提供了一个JavaScript代码片段,用于在网页中禁用特定键盘操作,如禁止按下Ctrl+N键。代码适用于IE浏览器,并能应用于网页的任意层级框架,确保所有加载的框架都受到限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[color=black][size=medium]废话不多说直接上代码 [/size][/color]
想直接用[color=darkred]?[/color]

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

我的这段代码只是为了禁用Ctrl+N 键的,所以只用了onkeydown事件,
当然你可以改成其他事件,也可以禁用其他键盘操作。
当然还有缺陷拉,请各位斧正!
[color=red]只适合IE,[/color]没空做兼容

<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值