Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does appending a <script> to a dynamically created <iframe> seem to run the script in the parent page?

I'm attempting to create an <iframe> using JavaScript, then append a <script> element to that <iframe>, which I want to run in the context of the <iframe>d document.

Unfortunately, it seems I'm doing something wrong - my JavaScript appears to execute successfully, but the context of the <script> is the parent page, not the <iframe>d document. I also get a 301 Error in Firebug's "Net" tab when the browser requests iframe_test.js, though it then requests it again (not sure why?) successfully.

This is the code I'm using (live demo at http://onespot.wsj.com/static/iframe_test.html):

iframe_test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">   <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>     <title>&lt;iframe&gt; test</title>   </head>   <body>     <div id="bucket"></div>     <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>     <script type="text/javascript">       $(document).ready(function() {         $('#bucket').append('<iframe id="test"></iframe>');         setTimeout(function() {           var iframe_body = $('#test').contents().find('body');           iframe_body.append('<scr' + 'ipt type="text/javascript" src="http://onespot.wsj.com/static/iframe_test.js"></scr' + 'ipt>');         }, 100);       });     </script>   </body> </html> 

iframe_test.js

$(function() {   var test = '<p>Shouldn\'t this be inside the &lt;iframe&gt;?</p>';   $('body').append(test); }); 

One thing that seems unusual is that the the code in iframe_test.js even works; I haven't loaded jQuery in the <iframe> itself, only in the parent document. That seems like a clue to me, but I can't figure out what it means.

Any ideas, suggestions, etc. would be much appreciated!

like image 580
Bungle Avatar asked Oct 19 '09 21:10

Bungle


People also ask

Can an iframe run JavaScript?

Calling a parent JS function from iframe is possible, but only when both the parent and the page loaded in the iframe are from same domain i.e. example.com , and both are using same protocol i.e. both are either on http:// or https:// .

What is #document in iframe?

Definition and Usage. The contentDocument property returns the Document object generated by a frame or iframe element. This property can be used in the host window to access the Document object that belongs to a frame or iframe element.


1 Answers

Had the same problem, took me hours to find the solution. You just need to create the script's object using the iframe's document.

var myIframe = document.getElementById("myIframeId"); var script = myIframe.contentWindow.document.createElement("script"); script.type = "text/javascript"; script.src = src; myIframe.contentWindow.document.body.appendChild(script); 

Works like a charm!

like image 153
Oleg Grishko Avatar answered Sep 23 '22 15:09

Oleg Grishko