js檢測(cè)iframe是否加載完成的方法
本文實(shí)例講述了js檢測(cè)iframe是否加載完成的方法。分享給大家供大家參考,具體如下:
這里是繼上一篇《js實(shí)現(xiàn)iframe框架取值的方法(兼容IE,firefox,chrome等)》的擴(kuò)展應(yīng)用:
應(yīng)用場(chǎng)景:iframe個(gè)人感覺最獨(dú)特的應(yīng)用之一就是配合P3P協(xié)議可以實(shí)現(xiàn)跨域?qū)懭隿ookie(好象除此之外,還沒找到更有效的辦法),但是有時(shí)候我們不知道這個(gè)iframe頁面是否執(zhí)行完畢,有沒有辦法判斷iframe里的頁面是否load完成了呢?
iframe1.html:
<html>
<head>
<title>框架內(nèi)頁</title>
</head>
<body>
<div>
<input id="txt" name="txt" type="text" value="3秒鐘后這里會(huì)變成ok" />
</div>
<script type="text/javascript">
setTimeout("SetValue()",3000);
function SetValue(){
document.getElementById("txt").value="ok";
}
</script>
</body>
</html>
iframe2.html:
<html>
<head>
<title>框架內(nèi)頁</title>
</head>
<body>
<div>
<input id="txt" name="txt" type="text" value="6秒鐘后這里會(huì)變成ok" />
</div>
<script type="text/javascript">
setTimeout("SetValue()",6000);
function SetValue(){
document.getElementById("txt").value="ok";
}
</script>
</body>
</html>
index.html:
<html>
<head>
<title>檢測(cè)本頁中的所有iframe是否加載完成</title>
<script type="text/javascript">
//得取iframe中的某個(gè)html控件值
function getIframeControlValue(iframeId,controlId){
var ofrm = document.getElementById(iframeId).document;
if (ofrm==undefined)
{
ofrm = document.getElementById(iframeId).contentWindow.document;
var ff = ofrm1.getElementById(controlId).value;
return ff;
}
else
{
var ie = document.frames[iframeId].document.getElementById(controlId).value;
return ie;
}
}
//檢測(cè)所有的iframe是否"加載"完成
function fnLoadOk(){
var b = true;
for(var i=1;i<=2;i++){
if (getIframeControlValue("frame" + i,"txt")=="ok"){
b = b && true;
}
else
{
b = b && false;
}
}
return b;
}
//設(shè)置回答顯示區(qū)的值
function setValue(str){
if (str!=null && str.length>0){
document.getElementById("result").innerHTML = "<span style='color:red'>" + new Date().toLocaleString() + " " + str + "</span>";
}
else{
document.getElementById("result").innerHTML = "<span style='color:green'>" + new Date().toLocaleString() + " 正在加載" + "</span>";
}
}
var _check = setInterval("t()",500);//每隔0.5秒檢查一次
function t(){
if (fnLoadOk()){
clearInterval(_check);//加載完成后,清除定時(shí)器
setValue("加載完成!");
}
else{
setValue();
}
}
</script>
</head>
<body>
<h3>檢測(cè)本頁中的iframe是否加載完成</h3>
<iframe name="frame1" id="frame1" src="iframe1.html" frameborder="1" height="60" width="180"></iframe>
<iframe name="frame2" id="frame2" src="iframe2.html" frameborder="1" height="60" width="180"></iframe>
<div id="result" style="margin:10px;">
準(zhǔn)備就緒
</div>
</body>
</html>
值得注意的是:本文中的示例是放在按鈕click事件中檢測(cè)的,如果打算頁面一打開就開始檢測(cè),一定要放在index.html頁body的onload事件中,否則會(huì)出異常(原因是index.html尚未加載完成,這時(shí)就急著獲取框架的內(nèi)容,得到的是undefined或null)
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- js下判斷 iframe 是否加載完成的完美方法
- JS加載iFrame出現(xiàn)空白問題的解決辦法
- 基于JS判斷iframe是否加載成功的方法(多種瀏覽器)
- JS iFrame加載慢怎么解決
- js通過iframe加載外部網(wǎng)頁的實(shí)現(xiàn)代碼
- javascript實(shí)現(xiàn)iframe框架延時(shí)加載的方法
- 動(dòng)態(tài)加載js、css等文件跨iframe實(shí)現(xiàn)
- javascript firefox 自動(dòng)加載iframe 自動(dòng)調(diào)整高寬示例
- js中頁面的重新加載(當(dāng)前頁面/上級(jí)頁面)及frame或iframe元素引用介紹
- javascript應(yīng)用:Iframe自適應(yīng)其加載的內(nèi)容高度
- JS判斷iframe是否加載完成的方法
相關(guān)文章
動(dòng)態(tài)載入/刪除/更新外部 JavaScript/Css 文件的代碼
動(dòng)態(tài)載入/刪除/更新外部 JavaScript/Css 文件的代碼2010-07-07
js樹插件zTree獲取所有選中節(jié)點(diǎn)數(shù)據(jù)的方法
這篇文章主要介紹了js樹插件zTree獲取所有選中節(jié)點(diǎn)數(shù)據(jù)的方法,是對(duì)js樹插件zTree非常實(shí)用的操作,需要的朋友可以參考下2015-01-01
總結(jié)兩個(gè)Javascript的哈稀對(duì)象的一些編程技巧
總結(jié)兩個(gè)Javascript的哈稀對(duì)象的一些編程技巧...2007-04-04

