views:

269

answers:

2

Hi, I've got a dynamically created iframe that I'm trying to add a script to. For example:

var iframe = document.createElement("iframe");

I'd like to inject javascript into this iframe. I've got this working without using jquery by creating a script element under the iframe object and adding callbacks to this script object via the readystate & onReadyState events. Now I'd like to try the same thing with jquery.

However if I just do:

$.getScript(url,function() {
 myCallback();
});

My script(s) get added to the current document, and references from the iframe object are broken. How can I call getscript on an iframe object?

Tried this which didn't work:

var elementID = $(element).attr('id');
$('iframe#' + elementID).getScript(url, function(){
 myCallback();
});
A: 

The only way .getScript would work is if you executed it on the iframe as jquery automatically downloads and adds the script to the current document

Gary Green
+1  A: 

This plugin allows you to inject jQuery & other scripts into an iFrame on load: http://ideamill.synaptrixgroup.com/?page%5Fid=18 and might come in handy.

DEfusion