I want to set the contents of an iframe dynamically, when no pages were loaded before in the iframe.
I'm doing that :
iframe = $('<iframe id="thepage"></iframe>');
iframeHtml = 'iframeHtml';
$('body').append(iframe);
iframe
.contents()
.html(iframeHtml);
But it is not working, the html is still empty.
If you want to avoid using
document.write
, which is generally not recommended any longer, you can get at the frame contents to add the content:the reason it does not work is because
contents()
returns adocument
object andhtml()
only works onHTMLElemnt
objects,html()
is basically a wrapper aroundinnerHTML
property anddocument
does not have such property.the best way to populate frame contents is
document.write
UPD: that is
Using JQuery, you can edit the iframe's attribute srcdoc by passing it the html as string: