views:

88

answers:

4

Hello everybody !

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 !

+1  A: 

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.

Ryan Brunner
I do server-side requests, that's why I use DOM
Lucas
+2  A: 

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

Pekka
Thanks a lot, I'm looking at all this stuff right now =)
Lucas
It seems to be what I need. Do you know if there is a way to create the code that goes into the HTML data using an Element or I don't know how, and then use it the the src ? Because my code is JavaScript and for the moment I can not do it directly. But it will try again.
Lucas
@Lucas jQuery's `.html()` should give you the HTML contents of any element. `encodeURIComponent()` should encode it properly for use in the first example. There is no native `base64()` implementation in JS but better go with the URIencoded way, as base64 bloats the code by 33%.
Pekka
In fact that is still not working but my current error is an "Unsafe JavaScript attempt to access frame with URL, Domains, protocols and ports must match" error. I will do other searches for that. But thanks =)
Lucas
@Lucas awww, that's not nice. Maybe worth another question, there should be a way around that if you create the element yourself.
Pekka
@Pekka the code I want to add into the iframe is a google ad, that call for a JS google code, which it itself using an iframe. That's why it doesn't like it I think. I think I saw somewhere that I could be able to fix this by setting the domain etc.I will try it by my own a bit then ask a question if I can't do it alone!
Lucas
Just in case, The full error is:"Unsafe JavaScript attempt to access frame with URL file:///home/lucas/Bureau/google_ad.html from frame with URL data:text/html, <script>google_comments</script> <script type="text/javascript" src="google_path.js"></script>. Domains, protocols and ports must match.""file:///home/lucas/Bureau/google_ad.html" is my experimental file to try things."<script>google_comments</script> <script type="text/javascript" src="google_path.js"></script>" is my (simplified) ad code.
Lucas
@Lucas first step, move away from `file://` urls to a local or remote web server. Browsers are awfully strict with those.
Pekka
@Pekka done that. And have still the same error !
Lucas
@Lucas I'd say that calls for a question of its own, with code and everything.
Pekka
@Pekka yes I have already done that here http://stackoverflow.com/questions/3473416/problem-to-display-dynamically-a-javascript-google-ad-in-an-iframe-using-jquery/3474598#3474598 (=
Lucas
A: 

Ryan Brunner has a good point. Try jQuery's load method. It even lets you load parts of a page. Say I want to load a table with id people into a div with id folks,

$('#folks').load('people.htm #people');

http://api.jquery.com/load/

And because it's jQuery, it works very well in all supported browsers. :)

sidewaysmilk
The question asks for an iframe without sourcing the data from a file … you recommend sourcing the data from a file without using an iframe. Isn't that a bit backwards?!
David Dorward
Point. I definitely misread that.
sidewaysmilk
A: 

following code should do what you want.

you can leave the src attribute empty.

var yourcontent="<div>your stuff</div>";
window.frames['frame_name'].document.write=yourcontent;
N30
I cannot do that after the page is loaded.
Lucas