javascript - Append <script> into <div> using jquery -
i'm trying append <script>
tag <div>
. whenever try this, script doesn't loads. works if hard coded under tag.
but if try append <img>
tag, works!
here's partial code give ideas.
$(document).ready(function() { var tag = '<script src="xxx.js"></script>'; // file grab images var tag2 = '<img src="some image link"></img>'; $('#test').append(tag); //testing $('#test2').append(tag2); //testing });
on html
<div id="test"></div> <!-- not working! --> <div id="test2"></div> <!-- works! --> <div id="test"><script src="xxx.js file grab images"></script></div> <!-- works too! -->
edit more info below:
i've looked solutions on net , of answer same ones given here. in case, different others. that's because tag variable must database.
<? $result = mysql_query("select code users_loc method='script'") or die(mysql_error()); $row = mysql_fetch_array($result); ?>
and on javascript side:
$(document).ready(function() { var tag = <? echo $row['code'] ?>; var tag2 = <? echo $row2['code'] ?>; $('#test').append(tag); //testing $('#test2').append(tag2); //testing });
on site, users input external image source database, , grab source , display on site. problem arise when user put in <script>
tag works if hard coded <div>
i kept having error message on console "a call document.write() asynchronously-loaded external script ignored."
i hope answers confusions. thanks.
edit: think problem append: does jquery append() behave asynchronously?
it because parsing problem. problem showing after found </script>
code. it's assume want close script tag won't. in order avoid that, put \
before /
escape special character :
$(document).ready(function() { var tag = '<script src="xxx.js"><\/script>'; // file grab images var tag2 = '<img src="some image link"></img>'; $('#test').append(tag); //testing $('#test2').append(tag2); //testing });
try inspect element in chrome see code.
Comments
Post a Comment