繁体   English   中英

动态加载HTML和iframe

[英]Dynamically loading HTML into and iframe

我正在尝试动态地将带有HTML的iframe添加到现有页面。 我已经得到了iframe加载和显示。 但是iframe的“正文”没有显示。 有帮助吗?

<html>
<head>
<script>
window.onload = function() {
                var iframe = document.createElement('iframe');
                var html =
                    '<html><head>' +
                        '<scr' + 'ipt src="https://localhost:44302/foo.js"></scr' + 'ipt>' +
                        '<scr' + 'ipt src="https://localhost:44302/bar.js"></scr' + 'ipt>' +
                    '</head>' +
                    '<body>Hi</body></html>';
                iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html);
                document.body.appendChild(iframe);
                console.log('iframe.contentWindow =', iframe.contentWindow);
            }
</script>
</head>
<body></body>
</html>

https://jsfiddle.net/0m5axpxx/2/

对不起,没有足够的代表直接评论。

var iframe = document.createElement('iframe');
var html =
    '<html>' + 
    '<head>' +
    '<scr' + 'ipt src="https://localhost:44302/oidc.js"></scr' + 'ipt>' +
    '<scr' + 'ipt src="https://localhost:44302/requestToken.js"></scr' + 'ipt>' +
    '</head>' +
    '<body>Hi</body>' + 
    '</html>';

iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html);
document.body.appendChild(iframe);
console.log('iframe.contentWindow =', iframe.contentWindow);

你在jsfiddle中的例子,'script'的第二行,是错误的srcipt ,修复它,它的工作原理。 的jsfiddle

 var iframe = document.createElement('iframe'); var html = '<html>' + '<head>' + '<scr' + 'ipt src="https://localhost:44302/oidc.js"></scr' + 'ipt>' + '<scr' + 'ipt src="https://localhost:44302/requestToken.js"></scr' + 'ipt>' + '</head>' + '<body>Hi</body>' + '</html>'; iframe.srcdoc = html; document.body.appendChild(iframe); console.log('iframe.contentWindow =', iframe.contentWindow); 

暂无
暂无

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

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