I have the following structure.
Hello World !!
I managed to do it with
var html_string= "content";
document.getElementById('output_iframe1').src = "data:text/html;charset=utf-8," + escape(html_string);
If you want to set an html content containg script tag to iframe, you have access to the iframe you created with:
$(iframeElement).contentWindow.document
so you can set innerHTML of any element in this.
But, for script tag, you should create and append an script tag, like this:
https://stackoverflow.com/a/13122011/4718434
You want to be using the iframe's srcdoc
attribute for that (MDN documentation).
var html_string = "<html><body><h1>My epic iframe</p></body></html>";
document.querySelector('iframe').srcdoc = html_string;
The nice thing about using this method over for example Red's method listed on this page, is that iframe contents added with srcdoc
are seen as the same-origin. That way can continue to manipulate and access the iframe with JavaScript if you wish.
You need -
var $frame = $('myiframe');
setTimeout( function() {
var doc = $frame[0].contentWindow.document;
var $body = $('body',doc);
$body.html('<div>Test_Div</div>');
}, 1 );
Code taken from - putting html inside an iframe (using javascript)
$('#myiframe').contents().find('html').html(s);
you can check from here http://jsfiddle.net/Y9beh/
Use the "contents" function:
$('#some-id').contents().find('html').html("some-html")
Relevant fiddle: http://jsfiddle.net/fDFca/