IE - IFRAMES/Data Uri

Hit*_*nds 8 javascript iframe internet-explorer data-uri

我需要在沙盒视图中显示内容,主要是一个完整的html文档(<html>...</html>).我正在使用带有src datauri的沙盒iframe .

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);
Run Code Online (Sandbox Code Playgroud)

不幸的是,Internet Explorer不支持...有没有解决方案/解决方法?

Hit*_*nds 4

我的解决方案:

  1. 创建一个空的index.html,只是为了具有同源 iframe。
  2. 通过javascript访问iframe
  3. 替换其内容

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);
Run Code Online (Sandbox Code Playgroud)
<iframe id="test" src="/index.html"></iframe>
Run Code Online (Sandbox Code Playgroud)