如何在iframe标记中插入javascript代码

muk*_*002 3 javascript iframe jquery

我这样做但不起作用: -

我试图在iframe中插入像素,然后将其附加到正文.

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
Run Code Online (Sandbox Code Playgroud)

Cer*_*rus 7

这样的脚本在</script>包含在字符串中时似乎会中断.解决方法可能是通过dom操作添加元素:

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'))
Run Code Online (Sandbox Code Playgroud)