English 中文(简体)
2. 动态地建立具有超文本的iframe
原标题:Creating an iframe with given HTML dynamically

I m trying to create an iframe from JavaScript and fill it with arbitrary HTML, like so:

var html =  <body>Foo</body> ;
var iframe = document.createElement( iframe );
iframe.src =  data:text/html;charset=utf-8,  + encodeURI(html);

我期待<代码>iframe,然后载有有效的窗口和文件。 然而,情况并非如此:

> console.log(iframe.contentWindow);
null

http://jsfiddle.net/TrevorBurnham/9k9Pe/>http://jsfiddle.net/TrevorBurnham/9k9Pe/

我所期待的是什么?

最佳回答
问题回答

如果您的<代码>src = encodeURI,我将采取不同的方式:

var iframe = document.createElement( iframe );
var html =  <body>Foo</body> ;
document.body.appendChild(iframe);
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(html);
iframe.contentWindow.document.close();

由于这没有X-omain的束缚,而且完全通过<条码>iframe处理,你可以在晚些时候查阅和操纵框架的内容。 你们都需要保证的是,内容已经提供,将(视浏览器类型而定)从发布指挥的期间/之后开始——但<>> /em>在发出<条码>时不曾做过。

采用100%的对应办法可以采用这种办法:

<html><body onload="parent.myCallbackFunc(this.window)"></body></html>

然而,机组有上载活动。 这里是利用内部的html作为OM(js):

iframe.onload = function() {
   var div=iframe.contentWindow.document.getElementById( mydiv );
};

我知道这是一个老的问题,但我认为,我会提供使用<条码>的示例,因为现在这个词是

利用srcdocvide, 您可提供在线超文本,以插入。 如果得到支持,则优先于<代码>src。 浏览器如果得不到支持,将回落到<条码><>>>。

我也建议使用sand Box因属性而对框架内容施加额外限制。 如果超文本不属你本人,这一点尤其重要。

const iframe = document.createElement( iframe );
const html =  <body>Foo</body> ;
iframe.srcdoc = html;
iframe.sandbox =   ;
document.body.appendChild(iframe);

如果你需要支持老的浏览器,你可以核对<条码>弧度/代码>的支持,并从其他答复中退回到另一种方法。

function setIframeHTML(iframe, html) {
  if (typeof iframe.srcdoc !==  undefined ) {
    iframe.srcdoc = html;
  } else {
    iframe.sandbox =  allow-same-origin ;
    iframe.contentWindow.document.open();
    iframe.contentWindow.document.write(html);
    iframe.contentWindow.document.close();
  }
}

var iframe = document.createElement( iframe );
iframe.sandbox =   ;
var html =  <body>Foo</body> ;

document.body.appendChild(iframe);
setIframeHTML(iframe, html);

由于你的伟大问题,这已使我陷入了几处困境。 在使用URI超文本来源数据时,我认为我必须确定一份完整的超文本文件。

下面是经修改的例子。

var html =  <html><head></head><body>Foo</body></html> ;
var iframe = document.createElement( iframe );
iframe.src =  data:text/html;charset=utf-8,  + encodeURI(html);

注意到以<代码><、html>、 tags和iframe.src string的html内容。

必须将iframe元素添加到DOM树上。

document.body.appendChild(iframe);

You will not be able to inspect the iframe.contentDocument unless you disable-web-security on your browser. You ll get a message

DOM 例外:没有读到超文本IFrameElement的内容文件: http:// localhost:7357” http:// localhost:7357

URL办法只能用于小型的超文本处理。 更为坚实的办法是,从一场博览中产生一种URL的物体,并将其用作动态框架的来源。

const html =  <html>...</html> ;
const iframe = document.createElement( iframe );
const blob = new Blob([html], {type:  text/html });
iframe.src = window.URL.createObjectURL(blob);
document.body.appendChild(iframe);

......

...
var el = document.getElementById( targetFrame );

var frame_win = getIframeWindow(el);

console.log(frame_win);
...

<>>>>> 此处定义的“植被”

function getIframeWindow(iframe_object) {
  var doc;

  if (iframe_object.contentWindow) {
    return iframe_object.contentWindow;
  }

  if (iframe_object.window) {
    return iframe_object.window;
  } 

  if (!doc && iframe_object.contentDocument) {
    doc = iframe_object.contentDocument;
  } 

  if (!doc && iframe_object.document) {
    doc = iframe_object.document;
  }

  if (doc && doc.defaultView) {
   return doc.defaultView;
  }

  if (doc && doc.parentWindow) {
    return doc.parentWindow;
  }

  return undefined;
}

(职能)-1

var frame = document.createElement( iframe );
frame.src =  https://1zr2h9xgfxqt.statuspage.io/embed/frame ;
frame.style.position =  fixed ;
frame.style.border =  none ;
frame.style.boxShadow =  0 20px 32px -8px rgba(9,20,66,0.25) ;
frame.style.zIndex =  9999 ;
frame.style.transition =  left 1s ease, bottom 1s ease, right 1s ease ;

var mobile;
if (mobile = screen.width < 450) {
  frame.src +=  ?mobile=true ;
  frame.style.height =  20vh ;
  frame.style.width =  100vw ;
  frame.style.left =  -9999px ;
  frame.style.bottom =  -9999px ;
  frame.style.transition =  bottom 1s ease ;
} else {
  frame.style.height =  115px ;
  frame.style.width =  320px ;
  frame.style.left =  auto ;
  frame.style.right =  -9999px ;
  frame.style.bottom =  60px ;
}

document.body.appendChild(frame);

var actions = {
  showFrame: function() {
    if (mobile) {
      frame.style.left =  0 ;
      frame.style.bottom =  0 ;
    }
    else {
      frame.style.left =  auto ;
      frame.style.right =  60px 
    }
  },
  dismissFrame: function(){
    frame.style.left =  -9999px ;
  }
}

window.addEventListener( message , function(event){
  if (event.data.action && actions.hasOwnProperty(event.data.action)) {
    actions[event.data.action](event.data);
  }
}, false);

window.statusEmbedTest = actions.showFrame;

(iii)





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签