2013-10-21 88 views
1

这里是我的iframe aspx页面onload事件

<asp:UpdatePanel ID="dynamicForm" runat="server" UpdateMode="Conditional"> 
      <ContentTemplate>      
       <iframe id="iTargetLoader" name="iTargetLoader" width="200" height="50" onload="onLoadFn()"></iframe> 
      </ContentTemplate> 
    </asp:UpdatePanel> 

这里是我的javascript iframe的onload事件

function onLoadFn() { 
     //debugger;   
     document.getElementById("iTargetLoader").contentWindow.document.defaultView.frameElement.innerText; 
     var frame = document.getElementById("iTargetLoader"); 
     var contentDoc = (frame.contentWindow || frame.contentDocument);   
     if (contentDoc != null && contentDoc.document.body != null) {    
      var serverResult = contentDoc.document.body.innerText;    
      var regSuccess = /success/i; 
      if (regSuccess.test(serverResult)) {     
       if (parseInt($('#ContentPlaceHolder1_hdn_qstnadd').val()) == 1) {      
         addQuestion();         
        $('#ContentPlaceHolder1_hdn_qstnadd').val(0); 
       }         
       //alert('success!'); 
       //return "success!"; 
      } 
      else { 
       if ($.trim(serverResult) == "") { 
        //alert("empty!"); 
       } 
       else { 
        alert("failed!" + serverResult + "Please Contact Your Administrator!"); 
        window.parent.window.open("", "_self"); 
        window.parent.window.close(); 
       } 
       //return "failure!"; 
      } 
     } 
     else { 
      return ""; 
     } 
    } 

这里onloadfn功能从一个servlet的反馈加载到iframe,即“iTargetLoader”我试图检查加载后的iframe的内容是否成功或不。它在IE中工作正常,但当我使用Firefox或谷歌浏览器onload事件加载的iframe没有完成,因此我nner html显示为空。但在IE中,加载首先完成,innerhtml显示反馈。我该如何解决它?

回答

1

所以你的问题更使其跨浏览器兼容: 让我们怪癖出来,然后......

HTML或ASPX

使用参考this所以你不需要多次获取该对象。

<iframe id="iTargetLoader" name="iTargetLoader" width="200" height="50" onload="javascript: onLoadFn(this);"></iframe> 

JS

// a global function needs to be defined first before it can be used 
// most likely not an issue in this example but 
// similar to addQuestion you might want to change it to IIFE 
// http://en.wikipedia.org/wiki/Immediately-invoked_function_expression 
function onLoadFn(frame) { 
    // define your variables 
    var contentDoc, 
     serverResult, 
     regSuccess = /success/i, 
     qstnadd; 

    // test your parameter exists, in this case the iframe (this) 
    if (frame) { 
     // Note: Because of security reasons, the contents of a document can be accessed from another document only if the two documents are located in the same domain. 
     // Note: Internet Explorer 8 (and higher) supports the contentDocument property only if a !DOCTYPE is specified. For earlier versions of IE, use the contentWindow property. 
     contentDoc = frame.contentWindow || frame.contentDocument; 

     if (contentDoc) { 
      // testing the proper document object here 
      if (contentDoc.document){ 
       contentDoc = contentDoc.document; 
      } 

      serverResult = contentDoc.body.innerText || ""; 

      if (regSuccess.test(serverResult)) { 
       // request your object once 
       // .Net supports something like IDMode static which can help to determine the ID here. 
       qstnadd = $('#ContentPlaceHolder1_hdn_qstnadd'); 
       // use a raddix when using parseInt 
       if (parseInt(qstnadd.val(), 10) === 1) { 
        addQuestion(); 
        qstnadd.val(0); 
       } 
      } else { 
       // looks trivial to me 
       if ($.trim(serverResult) === "") { 
        alert("empty"); 
       } else { 
        alert("failed!" + serverResult + "Please Contact Your Administrator!"); 
        window.parent.window.open("", "_self"); 
        window.parent.window.close(); 
       } 
      } 
     } 
    } 
} 

即使这不能解决您的问题直接,我敢肯定,你可以找出问题这样更好。胡乱猜测,你的问题是在contentDoc.document因为你请求window.document.bodydocument.body但不是window.document.document.body尽管后者可能在IE浏览器^^

+0

感谢的解释,但问题是contentDoc.document是空的,即除了IE之外,servlet反馈并没有加载到iframe中的contentDoc这个时候。 –

+0

所以你说'frame.contentWindow'和'frame.contentDocument'都是空的?如果是这种情况,则此代码之外还有另一个问题。 –

+1

是的,在加载事件中有问题。 IE和Google Chrome浏览器似乎对加载事件的行为不同。正如我前面所说的,IE在文档准备好之前加载了iframe,但最终加载了iframe。我怎样才能让谷歌Chrome浏览器像加载事件的IE一样行事? –