iframe的onload事件监听比较麻烦,浏览器注册事件监听的方式也不一样,下面是一个通用的监听方式。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<div id="cnt"></div>
<input name="bt" type="button" value="设置IFrame内容"
</body>
</html>
<script type='text/javascript'>
//添加两个
function doAction1() {
alert("
}
function doAction2() {
alert("
}
function doIt() {
//删除所有的ifream内容
var div = document.getElementById("cnt");
var arrifm = div.getElementsByTagName("iframe");
if (arrifm.length != 0) {
var x = div.removeChild(arrifm[0]);
alert("清除成功!")
}
//添加新的ifream内容
var iframe = document.createElement("iframe");
iframe.src = "b.html";
//为ifream的
if (iframe.attachEvent) {
iframe.attachEvent(" {
doAction1();
alert("Local iframe is now loaded 1.");
});
} else {
iframe.onload = function() {
this.doAction2();
alert("Local iframe is now loaded 2.");
}
}
//追加ifream元素
div.appendChild(iframe);
//弹出ifream的HTML代码
// alert(div.innerHTML);
}
</script>
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<div id="cnt"></div>
<input name="bt" type="button" value="设置IFrame内容"
</body>
</html>
<script type='text/javascript'>
//添加两个
function doAction1() {
alert("
}
function doAction2() {
alert("
}
function doIt() {
//删除所有的ifream内容
var div = document.getElementById("cnt");
var arrifm = div.getElementsByTagName("iframe");
if (arrifm.length != 0) {
var x = div.removeChild(arrifm[0]);
alert("清除成功!")
}
//添加新的ifream内容
var iframe = document.createElement("iframe");
iframe.src = "b.html";
//为ifream的
if (iframe.attachEvent) {
iframe.attachEvent(" {
doAction1();
alert("Local iframe is now loaded 1.");
});
} else {
iframe.onload = function() {
this.doAction2();
alert("Local iframe is now loaded 2.");
}
}
//追加ifream元素
div.appendChild(iframe);
//弹出ifream的HTML代码
// alert(div.innerHTML);
}
</script>
上面的事件监听写成下面的也行:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<div id="cnt"></div>
<input name="bt" type="button" value="设置IFrame内容"
</body>
</html>
<script type='text/javascript'>
//添加两个
function doAction1() {
alert("
}
function doAction2() {
alert("
}
function doIt() {
//删除所有的ifream内容
var div = document.getElementById("cnt");
var arrifm = div.getElementsByTagName("iframe");
if (arrifm.length != 0) {
var x = div.removeChild(arrifm[0]);
alert("清除成功!")
}
//添加新的ifream内容
var iframe = document.createElement("iframe");
iframe.src = "b.html";
//为ifream的
if (iframe.attachEvent) {
iframe.attachEvent(" doAction1);
} else {
iframe.onload = doAction2
}
//追加ifream元素
div.appendChild(iframe);
//弹出ifream的HTML代码
// alert(div.innerHTML);
}
</script>
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<div id="cnt"></div>
<input name="bt" type="button" value="设置IFrame内容"
</body>
</html>
<script type='text/javascript'>
//添加两个
function doAction1() {
alert("
}
function doAction2() {
alert("
}
function doIt() {
//删除所有的ifream内容
var div = document.getElementById("cnt");
var arrifm = div.getElementsByTagName("iframe");
if (arrifm.length != 0) {
var x = div.removeChild(arrifm[0]);
alert("清除成功!")
}
//添加新的ifream内容
var iframe = document.createElement("iframe");
iframe.src = "b.html";
//为ifream的
if (iframe.attachEvent) {
iframe.attachEvent(" doAction1);
} else {
iframe.onload = doAction2
}
//追加ifream元素
div.appendChild(iframe);
//弹出ifream的HTML代码
// alert(div.innerHTML);
}
</script>
但是下面这种方式不行:
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<div id="cnt"></div>
<input name="bt" type="button" value="设置IFrame内容" οnclick="doIt()">
</body>
</html>
<script type='text/javascript'>
function aa() {
alert("aaaaaaaaa");
}
function doIt() {
//删除所有的ifream内容
var div = document.getElementById("cnt");
var arrifm = div.getElementsByTagName("iframe");
if (arrifm.length != 0) {
var x = div.removeChild(arrifm[0]);
alert("清除成功!")
}
//添加新的ifream内容
var iframe = document.createElement("iframe");
iframe.src = "b.html";
//添加两个
doIt.prototype.doAction1 = function() {
alert("onload1");
bb();
};
doIt.prototype.doAction2 = function() {
alert("onload2");
bb();
};
// 下面的方式不行
if (iframe.attachEvent) {
iframe.attachEvent("onload", this.doAction1());
} else {
iframe.onload = this.doAction2();
}
// 下面的方式不行
// if (iframe.addEventListener) {
// iframe.addEventListener(' this.doAction1, false);
// } else if (iframe.attachEvent) {
// iframe.onload = this.doAction2;
// }
//追加ifream元素
div.appendChild(iframe);
//弹出ifream的HTML代码
alert(div.innerHTML);
}
</script>
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<div id="cnt"></div>
<input name="bt" type="button" value="设置IFrame内容" οnclick="doIt()">
</body>
</html>
<script type='text/javascript'>
function aa() {
alert("aaaaaaaaa");
}
function doIt() {
//删除所有的ifream内容
var div = document.getElementById("cnt");
var arrifm = div.getElementsByTagName("iframe");
if (arrifm.length != 0) {
var x = div.removeChild(arrifm[0]);
alert("清除成功!")
}
//添加新的ifream内容
var iframe = document.createElement("iframe");
iframe.src = "b.html";
//添加两个
doIt.prototype.doAction1 = function() {
alert("onload1");
bb();
};
doIt.prototype.doAction2 = function() {
alert("onload2");
bb();
};
// 下面的方式不行
if (iframe.attachEvent) {
iframe.attachEvent("onload", this.doAction1());
} else {
iframe.onload = this.doAction2();
}
// 下面的方式不行
// if (iframe.addEventListener) {
// iframe.addEventListener(' this.doAction1, false);
// } else if (iframe.attachEvent) {
// iframe.onload = this.doAction2;
// }
//追加ifream元素
div.appendChild(iframe);
//弹出ifream的HTML代码
alert(div.innerHTML);
}
</script>
注意:父窗口不能调用ifream内的js方法。
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/481054,如需转载请自行联系原作者