Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

putting html inside an iframe (using javascript)

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.

like image 392
hasen Avatar asked Mar 06 '09 23:03

hasen


People also ask

How do I add HTML content to an iframe?

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.

Can you use javascript in an iframe?

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.

Can you put a div inside an iframe?

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.

What is #document in iframe?

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.


1 Answers

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.

like image 184
VuesomeDev Avatar answered Sep 19 '22 00:09

VuesomeDev