Can I create an empty iframe as a placeholder to later insert html into it?
In otherwords, suppose I have an empty iframe with an id,
How do I insert html in it?
I'm using jquery, if that makes it easier.
Answer: Use the jQuery contents() method You can use the jQuery contents() method in combination with the find() , val() and html() methods to insert text or HTML inside an iframe body.
As long as the protocol, domain and port of the parent page and iframe match, everything will work fine. I added an example on how to use the window.
Approach 1: For adding additional div's in an iframe, you need to use a wrapper div, that wraps the contents of your intended div and the iframe into one unit. This way you can display the contents of your div along with the iframe embedding to the document/webpage.
Definition and Usage. The contentDocument property returns the Document object generated by a frame or iframe element. This property can be used in the host window to access the Document object that belongs to a frame or iframe element.
You can do it without jQuery also:
var iframe = document.getElementById('iframeID'); iframe = iframe.contentWindow || ( iframe.contentDocument.document || iframe.contentDocument); iframe.document.open(); iframe.document.write('Hello World!'); iframe.document.close();
jQuery's html strips body, html and head tags from the inserted HTML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With