jquery appendTo removes javascript script tags
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.
精彩评论