Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert javascript code in iframe tag

I am doing like this but not working:-

I am trying to insert the pixel in iframe and then append it to the body.

jQuery('#subscribr').append('<iframe>
<script language="javascript" src="http://www.abc/static/st.v2.js"></script>
<script language="javascript">
var ef_event_type="transaction";
var ef_transaction_properties = "ev_ProductViews=1";
/*
 * Do not modify below this line
 */
var ef_segment = "";
var ef_search_segment = "";
var ef_userid="xxxx";
var ef_pixel_host="pixel.abc.net";
var ef_fb_is_app = 0;
effp();
</script>
</iframe>');
enter code here
like image 535
mukund002 Avatar asked Nov 30 '12 10:11

mukund002


People also ask

Can you put JavaScript in an iframe?

Either your src-url will be used or your code between the iframe-tags will be used; never both. This means that there is no way to "embed a javascript into an iframe". The only way to get your script in there would be to add it to the source page that you're loading into the iframe.

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.

How do you insert JavaScript into HTML?

You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.

How do I create an iframe embed code?

To embed an iframe in a content page, select Interactive layout, choose the HTML block and paste the iframe code there. You can adjust the iframe width and height properties. To embed an iframe using the Old (Classic) editor, click on the Embed Media icon and paste the code in the appropriate field.


3 Answers

Scripts like that appear to break when </script> is included in the string. A workaround to that could be to add the elements through dom manipulation:

iframe = $('<iframe>');

var script   = document.createElement("script");
script.type  = "text/javascript";
script.src   = "http://www.abc/static/st.v2.js"; // Or:
script.text  = "Your code here!"

iframe[0].appendChild(script);
iframe.appendTo('#subscribr'))
like image 174
Cerbrus Avatar answered Oct 06 '22 04:10

Cerbrus


This is the solution which worked for me..

var s = document.createElement("script");
    s.type = "text/javascript";
    s.src = "http://www.abc/static/st.v2.js";
    // Use selector here
    jQuery("#subscribr").append(s);
like image 42
mukund002 Avatar answered Oct 06 '22 04:10

mukund002


Working sample: http://fiddle.jshell.net/f2x7G/

var doc = null;
   if(iframe.contentDocument) doc = iframe.contentDocument;
   else if(iframe.contentWindow) doc = iframe.contentWindow.document;
   else if(iframe.document) doc = iframe.document;

if(doc == null) throw "Document not initialized";

doc.open();
var script   = doc.createElement("script");
script.type  = "text/javascript";
// script.src = 'http://www.abc/static/st.v2.js';
script.text  = "alert('voila!');"
doc.appendChild(script);
doc.close();
like image 25
Boris Gappov Avatar answered Oct 06 '22 05:10

Boris Gappov