views:

47

answers:

3

I've got the logic working to append into my iframe from the parent

this works:

$('#iframe').load(function() {
  $(this).contents().find('#target').append('this text has been inserted into the iframe by jquery');    
});

this doesn't

$('#iframe').load(function() {
  $(this).contents().find('body').append('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;');    
});

.lf

The problem is something to do with the inserted script tags not being escaped properly. Half of the javascript is becomes visible in the html, like the first script tag has been abruptly ended.

+1  A: 

I'm a bit surprised that isn't working [Edit: No longer surprised at all, see mtrovo's answer.]...but here's what I do, which is mostly non-jQuery per your comment below but still quite brief:

var rawframe = document.getElementById('theframe');
var doc = rawframe.contentDocument;
if (!doc && rawframe.contentWindow) {
    doc = rawframe.contentWindow.document;
}
var script = doc.createElement('script');
script.type = "text/javascript";
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js";
document.body.appendChild(script);

Off-topic: I really wouldn't give an iframe (or anything else) the ID "iframe". That just feels like it's asking for trouble (IE has namespace issues, and while I'm not aware of it confusing tag names and IDs, I wouldn't be completely shocked). I've used "theframe" above instead.

T.J. Crowder
Thanks TJ, this is looking good, I've just come across the problem that i need to load jquery into the iframe, without using jquery to do so. (because jquery isn't yet loaded into the iframe to do the loading) ! - is it easy to do this in pure js?
Haroldo
@Haroldo: Yes. In fact, the answer was pure JavaScript except for the first line. I've modified the first line so it's pure JavaScript. But you can use jQuery to do the load, provided jQuery is loaded in the window your code is running in (as opposed to the iframe's window). But I think mtrovo has pointed out why your original code wasn't working, see [his/her answer](http://stackoverflow.com/questions/3935398/how-can-i-load-scripts-into-an-iframe/3935797#3935797) for more.
T.J. Crowder
thanks for all your help TJ
Haroldo
+1  A: 

i just thought i should share this plugin as well, in case anyone would be interested

http://ideamill.synaptrixgroup.com/?page_id=18

Mouhannad
+2  A: 

Maybe the error is with your string, never create a string in javascript with a literal < /script> in it.

$('#iframe').load(function() {
  $(this).contents().find('body').append('<scr' + 'ipt type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/scr' + 'ipt>');    
});
mtrovo
Gah! How could I miss that?! I even pointed it out to someone else in another question (not on SO) *earlier today*. Wish I could +2 this.
T.J. Crowder
*"...never create a string with..."* Well, *creating* it (as you have done) is fine. It's having the literal one there that's the problem.
T.J. Crowder
i was nearly there in the question "The problem is something to do with the inserted script tags not being escaped properly" haha, thanks guys
Haroldo