开发者

How to insert html in iframe

开发者 https://www.devze.com 2022-12-29 22:32 出处:网络
Hallo all: I 开发者_如何学JAVAneed to insert a html string in a iframe as shown below: .... var html = \"<html><head><title>Titolo</title></head><body><p>bo

Hallo all: I 开发者_如何学JAVAneed to insert a html string in a iframe as shown below:

....

var html = "<html><head><title>Titolo</title></head><body><p>body</p></body></html>"

jQuery('#popolaIframe').click(function() {
  parent.$("#indexIframe")[0].documentElement.innerHTML = html;     
}); 

Is there a way to achieve this?


var html = "<html><head><title>Titolo</title></head><body><p>body</p></body></html>"

jQuery('#popolaIframe').click(function() {
  var doc = parent.$("#indexIframe")[0].documentElement;
  doc.open();
  doc.write(html);
  doc.close();     
}); 


Does that code you posted work? If not, it's probably because browsers disallow modification of iframe content for security reasons.


Looks like you can get a refrence to the body so I don't see why not:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_iframe_contentdocument


You have lost 1 level, you need modify innerHtml of body, but not document:

document.body.innerHTML = bla-bla


You cannot insert into an iframe unless you remove() the iframe and use 'append(html)'. You can insert it inside iframes body like

$('body',parent.$("#indexIframe")[0].contentWindow.document).html(html)


Alternitevely if not sure about the parent element you could do that:

var doc = $.find("#myIframe")[0].contentWindow.document; //that will look in the whole dom though
doc.open();
doc.write(html);

At least this did the job in my case :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号