Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to "fake" the src attribute of an iframe?

I try to add dynamically an iframe to a web page using JavaScript. I would like to know if that's possible to set the src attribute of my iframe without using another html file via an URL. I mean is there a way to "fake" the html for the src attribute file with a JS variable where we I can set my code (which is JS itself) ? I would use the DOM createElement to create the iframe in jQuery.

Thanks !

like image 265
Lucas Avatar asked Aug 11 '10 20:08

Lucas


People also ask

What is the SRC attribute in iframe?

The HTML <iframe> src attribute is used to specify the URL of the document that are embedded to the <iframe> element. Attribute Values: It contains single value URL which specifies the URL of the document that is embedded to the iframe.

Can iframe be hidden?

The hidden attribute hides the <iframe> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <iframe> is not visible, but maintains its position on the page.


2 Answers

You could look into data:URIs.

<iframe src="data:text/html, .... URLencoded HTML data ....">

alternatively

<iframe src="data:text/html;base64, .... base64 encoded HTML data ....">

The scheme is supported by IE >= 8 (MSDN source), Firefox, Safari 3+ and Opera.

It has differing length limits. Opera is said to cut off at about 4 kilobytes; Internet Explorer at 32 kilobytes. Firefox has no explicit length limit.

More on data URIs and tools for converting at Mozilla Developer Central

like image 184
Pekka Avatar answered Oct 13 '22 19:10

Pekka


If you're controlling the iframe entirely client-side, and never need to make server-side requests with that iframe, it's proably easier to style a div to appear like an iframe (look into the overflow property), where you'll have far simpler and more direct control to the DOM contents of that div.

like image 42
Ryan Brunner Avatar answered Oct 13 '22 18:10

Ryan Brunner