1

I am receiving from server full HTML code of different pages as string:

 $.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);
    })

;

Setting dynamic content to iframe method 1:

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

Setting dynamic content to iframe method 2:

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

Setting dynamic content to iframe method 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.');
   }

The problem is that some pages displaying well with method 1, some with method 2 and some with method 3, but any of them do not approach to all web pages. Please help

Geha
  • 1,345
  • 2
  • 10
  • 16
  • I think this example will do the work [example1](http://stackoverflow.com/a/620905) Try to uncoment //iframedoc.open(); in third method. It seems that third method is optimal – Valeriy Blyus Sep 06 '16 at 14:56

1 Answers1

0

Try to modify your third method like this:

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();