亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

js檢測iframe是否加載完成的方法

 更新時(shí)間:2015年11月26日 15:37:02   作者:Jimmy.Yang  
這篇文章主要介紹了js檢測iframe是否加載完成的方法,涉及JavaScript針對(duì)框架頁面控件值的判斷與頁面加載的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了js檢測iframe是否加載完成的方法。分享給大家供大家參考,具體如下:

這里是繼上一篇《js實(shí)現(xiàn)iframe框架取值的方法(兼容IE,firefox,chrome等)》的擴(kuò)展應(yīng)用:

應(yī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>檢測本頁中的所有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;
  } 
}
//檢測所有的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>檢測本頁中的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事件中檢測的,如果打算頁面一打開就開始檢測,一定要放在index.html頁body的onload事件中,否則會(huì)出異常(原因是index.html尚未加載完成,這時(shí)就急著獲取框架的內(nèi)容,得到的是undefined或null)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論