js檢測iframe是否加載完成的方法
本文實例講述了js檢測iframe是否加載完成的方法。分享給大家供大家參考,具體如下:
這里是繼上一篇《js實現(xiàn)iframe框架取值的方法(兼容IE,firefox,chrome等)》的擴展應(yīng)用:
應(yīng)用場景:iframe個人感覺最獨特的應(yīng)用之一就是配合P3P協(xié)議可以實現(xiàn)跨域?qū)懭隿ookie(好象除此之外,還沒找到更有效的辦法),但是有時候我們不知道這個iframe頁面是否執(zhí)行完畢,有沒有辦法判斷iframe里的頁面是否load完成了呢?
iframe1.html:
<html>
<head>
<title>框架內(nèi)頁</title>
</head>
<body>
<div>
<input id="txt" name="txt" type="text" value="3秒鐘后這里會變成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秒鐘后這里會變成ok" />
</div>
<script type="text/javascript">
setTimeout("SetValue()",6000);
function SetValue(){
document.getElementById("txt").value="ok";
}
</script>
</body>
</html>
index.html:
<html>
<head>
<title>檢測本頁中的所有iframe是否加載完成</title>
<script type="text/javascript">
//得取iframe中的某個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;
}
}
//檢測所有的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);//加載完成后,清除定時器
setValue("加載完成!");
}
else{
setValue();
}
}
</script>
</head>
<body>
<h3>檢測本頁中的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;">
準備就緒
</div>
</body>
</html>
值得注意的是:本文中的示例是放在按鈕click事件中檢測的,如果打算頁面一打開就開始檢測,一定要放在index.html頁body的onload事件中,否則會出異常(原因是index.html尚未加載完成,這時就急著獲取框架的內(nèi)容,得到的是undefined或null)
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
- js下判斷 iframe 是否加載完成的完美方法
- JS加載iFrame出現(xiàn)空白問題的解決辦法
- 基于JS判斷iframe是否加載成功的方法(多種瀏覽器)
- JS iFrame加載慢怎么解決
- js通過iframe加載外部網(wǎng)頁的實現(xiàn)代碼
- javascript實現(xiàn)iframe框架延時加載的方法
- 動態(tài)加載js、css等文件跨iframe實現(xiàn)
- javascript firefox 自動加載iframe 自動調(diào)整高寬示例
- js中頁面的重新加載(當前頁面/上級頁面)及frame或iframe元素引用介紹
- javascript應(yīng)用:Iframe自適應(yīng)其加載的內(nèi)容高度
- JS判斷iframe是否加載完成的方法
相關(guān)文章
動態(tài)載入/刪除/更新外部 JavaScript/Css 文件的代碼
動態(tài)載入/刪除/更新外部 JavaScript/Css 文件的代碼2010-07-07
js樹插件zTree獲取所有選中節(jié)點數(shù)據(jù)的方法
這篇文章主要介紹了js樹插件zTree獲取所有選中節(jié)點數(shù)據(jù)的方法,是對js樹插件zTree非常實用的操作,需要的朋友可以參考下2015-01-01
總結(jié)兩個Javascript的哈稀對象的一些編程技巧
總結(jié)兩個Javascript的哈稀對象的一些編程技巧...2007-04-04

