开发者

jquery appendTo removes javascript script tags

开发者 https://www.devze.com 2022-12-13 09:06 出处:网络
var testdiv = \'<div id=\"hello\"><p>hiii</p> <script type=\"text/javascript\">some javascript functions</script>
var testdiv = '<div id="hello"><p>hiii</p>
               <script type="text/javascript">some javascript functions</script>
               </div&g开发者_开发技巧t;';
$(testdiv).appendTo(document.body);

After running above code, div that is added is missing javascript, i.e everything inside < script type="text/javascript" >

Is this known issue with appendTo ?


You might need to chop up the <script> tags. This works:

var testdiv = '<div id="hello"><p>hiii</p><scr'+'ipt type="text/javascript">console.log("buu")</scr'+'ipt></div>';
$(testdiv).appendTo(document.body);


var testdiv = '<div id="hello"><p>hiii</p><script type="text/javascript">alert(\'foo\')</script></div>';
$(testdiv).appendTo(document.body);

I replaced "some javascript" with an alert, and it works fine for me. Note that I had to escape the quotes I was using in the string, that may have been it.

I agree with jonscottclark about getScript(). There are indeed much better ways to include and execute additional scripts.

0

精彩评论

暂无评论...
验证码 换一张
取 消