Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i load <script>'s into an iframe?

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"></script>');    
});

.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.

like image 950
Haroldo Avatar asked Oct 14 '10 16:10

Haroldo


People also ask

Can you put a script in an iframe?

If a service uses a script to add an iframe you can add the <script>… </script> tag to the HTML section. This typically works, but because every integration is different you might have to wrap it yourself with a bit of code.

How do I add content to an iframe?

Answer: Use the jQuery contents() method You can use the jQuery contents() method in combination with the find() , val() and html() methods to insert text or HTML inside an iframe body.

Can we access Dom in iframe?

The contentWindow property returns the Window object of an HTMLIFrameElement. You can use this Window object to access the iframe's document and its internal DOM.

Can iframe load local files?

You can't load local files from your computer through the server. You should upload the test file to your server to and then load this file in an iframe.


1 Answers

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"></scr' + 'ipt>');    
});
like image 70
mtrovo Avatar answered Oct 15 '22 14:10

mtrovo