Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
15
rated 0 times [  18] [ 3]  / answers: 1 / hits: 22056  / 13 Years ago, fri, february 3, 2012, 12:00:00

FINAL UPDATE: The second code block is working--I just miss-typed the URL.



I'm trying to place a dynamically created script into an iframe. I can access the head of the iframe, but it doesn't seem to accept the append request. The console log call returns the head tag, and the following append of text into the body works fine. I've also tried appending the script tag to the body. (note: the script tag is not on the same domain, so I've avoided using getScript or ajax).



$(document).ready(function() {
$('<iframe></iframe>', {
name:contentFrame
,id:contentFrame
}).css(height, 300).css(width, 500).load(function() {
var script = document.createElement(script);
script.type = text/javascript;
script.src = //cross-domain.com/script.js;
console.log($(this).contents().find(head)); // outputs: [<head></head>]
$(this).contents().find(head).append(script); // doesn't work
$(this).contents().find(body).append(script); // doesn't work
$(this).contents().find(body).append(Test); // works
}).appendTo(#content);
});


Here is the new version that successfully appends the script based upon the suggestion below, but the script doesn't evaluate (as tested by a simple alert). Plus, another oddity: the working icon displays for about 15 seconds and the status (in chrome) says retrieving... Eventually it stops, but nothing happens.



    $(document).ready(function() {
$('<iframe></iframe>', {
name:contentFrame
,id:contentFrame
}).css(height, 300).css(width, 500).load(function() {
var script = document.createElement(script);
script.type = text/javascript;
script.src = //cross-domain.com/script.js;
console.log($(this).contents().find(head));
$(this).contents().find(head)[0].appendChild(script);
$(this).contents().find(body).append(Test);
}).appendTo(#content);
});


Update: When the cursor icon finally finishes working (about 10 seconds, which is odd because it's a local server with one line of code), I get this message in the Chrome console (it's in all red with a red circle X icon next to it):



GET http://cross-domain.com/script.js 
(anonymous function)temp.html:16
jQuery.event.dispatchjquery-1.7.1.js:3261
jQuery.event.add.elemData.handle.eventHandlejquery-1.7.1.js:2880
jQuery.fn.extend.appendjquery-1.7.1.js:5771
jQuery.fn.extend.domManipjquery-1.7.1.js:5973
jQuery.fn.extend.appendjquery-1.7.1.js:5769
jQuery.each.jQuery.fn.(anonymous function)jquery-1.7.1.js:6162
(anonymous function)temp.html:18
jQuery.Callbacks.firejquery-1.7.1.js:1049
jQuery.Callbacks.self.fireWithjquery-1.7.1.js:1167
jQuery.extend.readyjquery-1.7.1.js:435
DOMContentLoaded

More From » jquery

 Answers
103

See this answer https://stackoverflow.com/a/3603496/220299



It may in fact be working, but the script tag is just not visible.


[#87658] Wednesday, February 1, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gleng

Total Points: 471
Total Questions: 107
Total Answers: 102

Location: Virgin Islands (U.S.)
Member since Fri, May 7, 2021
3 Years ago
;