I need to display content in a sandboxed view, mostly a full html document (<html>...</html>
). I'm using a sandboxed iframe with src datauri.
var
iframe = document.createElement('iframe'),
content = '<html><head></head><body><h1>Hello</h1></body></html>'
;
iframe.sandbox = '';
iframe.src = 'data:text/html;charset=utf-8,' + content;
document.body.appendChild(iframe);
I need to display content in a sandboxed view, mostly a full html document (<html>...</html>
). I'm using a sandboxed iframe with src datauri.
var
iframe = document.createElement('iframe'),
content = '<html><head></head><body><h1>Hello</h1></body></html>'
;
iframe.sandbox = '';
iframe.src = 'data:text/html;charset=utf-8,' + content;
document.body.appendChild(iframe);
Unfortunately, that isn't supported in Internet Explorer... Is there any solution/workaround?
Share Improve this question edited Aug 22, 2016 at 9:35 Hitmands asked Aug 22, 2016 at 9:13 HitmandsHitmands 14.2k4 gold badges40 silver badges77 bronze badges2 Answers
Reset to default 4My solution:
- Create a empty
index.html
, just for having a same origin iframe. - Access the iframe via javascript
- Replace its content
function ReplaceIframeContentCtrl() {
var iframe = document.getElementById('test');
var content = "<html><head></head><body><h1>Hello</h1></body></html>";
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(content);
iframe.contentWindow.document.close();
}
document.addEventListener('DOMContentLoaded', ReplaceIframeContentCtrl);
<iframe id="test" src="/index.html"></iframe>
Simply creating an empty iframe and replacing it's content will do:
function insertIframeHtml(parent, html) {
const jparent=$(parent).empty();
const iframe=$('<iframe></iframe>').appendTo(jparent)[0];
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(html);
iframe.contentWindow.document.close();
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745393522a4625773.html
评论列表(0条)