javascript - IE - IFRAMESData Uri - Stack Overflow

I need to display content in a sandboxed view, mostly a full html document (<html>...<html>

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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

My solution:

  1. Create a empty index.html, just for having a same origin iframe.
  2. Access the iframe via javascript
  3. 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

相关推荐

  • javascript - IE - IFRAMESData Uri - Stack Overflow

    I need to display content in a sandboxed view, mostly a full html document (<html>...<html>

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信