2011-12-16 167 views
0

使用iframe發佈提交,它具有文件上傳。一切工作正常直到返回的內容。iframe提交獲取發佈的內容

如果我在頁面上顯示iframe,我看到從我的iframe中的函數返回的HTML。獲取iframe的內容以替換頁面的內容時遇到很多問題。

基本上獲取body iframe的內容並將其放置在頁面的body標籤中。 我確定它的1或2行,但是我在網上找到的每個jquery或document或getElementById想法都無法正常工作。

我注意到一些奇怪的事情,如果我嘗試在我的重裝功能中使用「console.info('somemessage')」,它會拋出一個錯誤,表示控制檯不存在。不知道爲什麼,但似乎我的JavaScript焦點是在iframe中,並看不到螢火蟲。

繼承人代碼被剝離。回到嘗試正在工作的.load事件。但是它的第二條命令是將內容寫入身體。當我評論它,iframe顯示,與我的內容。如果我運行它取消註釋,整個頁面重新加載。

 if (isStarted == false) { 
      isStarted = true; 
      statustracker.start(); 
      //style="height:0px;width:0px;" 
      var iframe = $('<iframe name="postframe" id="postframe" class="hidden" />'); 
      $('div#iframe').append(iframe); 

      $('#ImportDetailForm').attr("target", "postframe") 
      form.submit(); 

      $("#postframe").load(function() { 
       iframeContents = $("iframe")[0].contentDocument.body.innerHTML; 
       $("body").html(iframeContents); // <--- the problem 
      }); 
     } 
+0

所有表單設置(attr)都在表單標籤中。表單對象是jquery validate submithandler中的一個對象。 – 2011-12-16 16:46:05

回答

0

在你父頁面(包含iframe中的一個),你可以設置一個事件偵聽器「信息」事件,就像這樣:

window.addEventListener("message", receiveMessage, false); 
function receiveMessage(e) { ... } 

然後在你的iframe,只是發佈消息是這樣的「父」窗口:

parent.postMessage(someString, parentUrl); 

因爲消息是字符串,你需要序列化的任何數據你iframe和父窗口之間發送 - 我建議JSON!所以在你的情況下,從上傳請求序列化返回的HTML並將其發佈給父代,然後在該端反序列化並將其注入到DOM中。

+0

我無法弄清楚如何獲取序列化的iframe內容。試圖使用jquerys/json .serialize()但不起作用。 – 2011-12-16 17:07:36

0

無效或非法字符串指定「代碼:」 12

可能是因爲iframeContents是空的。

  $("#postframe").load(function() { 
       var win = document.getElementById("postframe").contentWindow; 
       var parent_url = decodeURIComponent(document.location.hash.replace(/^#/, '')), link; 
       iframeContents = $("#postframe").find("body").contents().serialize(); //$("iframe")[0].contentDocument.body.innerHTML; 
       console.info(iframeContents); 
       //$(this).parent("body").html(iframeContents); 
       win.postMessage(iframeContents, parent_url, parent); 
      });