繁体   English   中英

将动态内容设置为iframe

[英]Set dynamic content to iframe

我从服务器接收不同页面的完整HTML代码作为字符串:

 $.post($form.attr("action"), $form.serialize(), function(responseText) {
      console.log("text received");

      //Setting dynamic content to iframe method *

    }).error(function(p1, p2, p3){
      alert("error!");
      console.log(p1 + p2 + p3);
    })

;

将动态内容设置为iframe方法1:

var s = $(responseText);
$('#FileFrame').contents().find('html').html(s);

将动态内容设置为iframe方法2:

var $frame = $('#FileFrame');
  var doc = $frame[0].contentWindow.document;
  var $body = $('body',doc);
  $body.html(responseText);

将动态内容设置为iframe方法3:

var iframe = document.getElementById('FileFrame');
   var iframedoc = iframe.document;
   if (iframe.contentDocument)
   {        iframedoc = iframe.contentDocument;
   console.log("iframe has contentDocument");
   }
   else if (iframe.contentWindow)
   {
   iframedoc = iframe.contentWindow.document;
   console.log("iframe has contentWindow.document");
   }
   if (iframedoc) {
   //iframedoc.open();
   iframedoc.write(responseText);
   iframedoc.close();
   console.log("iframedoc is not NULL");
   } else {
   alert('Cannot inject dynamic contents into iframe.');
   }

问题是一些页面用方法1很好地显示,一些用方法2显示,一些用方法3显示,但是它们中的任何一个都不接近所有网页。 请帮忙

尝试修改你的第三种方法,如下所示:

var iframe = document.getElementById('FileFrame');
var iframeDoc;
if(iframe.contentWindow){
   iframeDoc = iframe.contentWindow;
}
else if(iframe.contentDocument.document){
   iframeDoc = iframe.contentDocument.document;
}
else if (iframe.contentDocument) {
   iframeDoc = iframe.contentDocument;
}
else{
   alert('Cannot inject dynamic contents into iframe.');
   return;
}
iframeDoc.document.open();
iframeDoc.document.write(responseText);
iframeDoc.document.close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM