2011-09-25 103 views
0

编辑:从测试看来,iframe会出现异步加载(尽管不完全确定)。我通过在700毫秒&工作后调用该函数来修复它。所以这让我觉得他们是异步的。我这样做:IFrame异步加载?如果不是,为什么会出现这种错误?

insertHTMLIntoIFrame(HTML); //$("#updaterIframe").contentWindow.location.reload(true); 
setTimeout("convertToUpdatable()", 700); 

末编辑

我有一些奇怪的发生的历史与我在我的网页的iframe。我的功能搜索我的iframe的所有HTML元素与类“可更新”&将这些元素转换为textareas。

我的问题:如果我打电话我已经插入一些HTML到iframe那么函数没有找到任何可更新的元素(当它们的存在在iframe)之后的功能

但是

如果我通过显示alert()来延迟函数执行。在搜索可更新元素的iframe之前,我确实发现&转换iframe中的所有元素。

这让我觉得iframe加载异常,是否正确?如果不是什么问题?有没有办法刷新iframe或确保我不调用我的函数,直到整个iframe加载?

// I call the functions in the following order: 
insertHTMLIntoIFrame("blah"); 
convertToUpdatable(); 

function convertToUpdatable() 
{ 
    // Post: Convert all HTML elements (with the class 'updatable') to textarea HTML elements 
    //  and store their HTML element type in the class attribute 
    // EG: Before: <p class="updatable Paragraph1"/> Hello this is some text 1 </p> 
    //  After : <p class='updatableElementTitle'>Paragraph1</p><textarea class="updatable Paragraph1 p"/> Hello this is some text 1 </textarea> 

    if (STATE != 1) { return; } 

    // if I dont have this line then I cant find any updatable elements in the iframe 
    alert("Displaying website with updatable regions"); 

    $("#updaterIframe").contents().find(".updatable").each(function() 
     { 
      var className = this.className; 
      var nodeName = this.nodeName; 
      var title  = getTitleName(this); 
      $(this).replaceWith("<p class='updatableElementTitle'>"+title+"</p><textarea class='"+ className + " " + nodeName +"'>"+$(this).html() +"</textarea>"); 
     }); 

    STATE = 0; 
} 

    function insertHTMLIntoIFrame(htmlSrc) 
{ 
    try 
    { 
     var ifrm = document.getElementById("updaterIframe"); 
     ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument; 
     ifrm.document.open(); 
     ifrm.document.write(htmlSrc); 
     ifrm.document.close(); 
    } 
    catch (ex) { alert("In insertHTMLIntoIFrame(): "+ex); } 
} 

回答

1

是的iframe加载,实际上在浏览器中加载的任何东西都是异步的。考虑大多数甚至有味道的事情,在js中是异步的,你的生活将变得更加容易。

忘记setTimeouts,而是绑定到事件,在这种情况下iframe对象上的load事件。

相关问题