gpt4 book ai didi

javascript - 将动态内容设置为 iframe

转载 作者:行者123 更新时间:2023-11-28 05:36:13 24 4
gpt4 key购买 nike

我从服务器接收不同页面的完整 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显示良好,但都不能覆盖所有网页。请帮忙

最佳答案

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

关于javascript - 将动态内容设置为 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39336149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com