javascript - Execute a src script inserted with .innerHTML -
i'm working on blogger. have function (showmusic, takes json code parameter) show posts , i'm calling this:
<script src='/feeds/posts/default/-/album/pop?max-results=20&orderby=published&alt=json-in-script&callback=showmusic'></script>
i'm looking way change labels ("album" , "pop") <select>
tags , need insert script innerhtml, script doesn't execute:
function myfunction(label1,label2) { var label1 += "/"; var label2 += "/"; var script = "\x3cscript src='/feeds/posts/default/-/"+label1+label2+"?max-results=20&orderby=published&alt=json-in-script&callback=showmusic'>\x3c/script>" document.getelementbyid("my-container").innerhtml = script; }
try out:
function myfunction( label1, label2 ) { var s = document.createelement( "script" ); s.type = "text/javascript"; s.async = true; s.src = "/feeds/posts/default/-/" + label1 + label2 + "?max-results=20&orderby=published&alt=json-in-script&callback=showmusic"; document.getelementbyid( "my-container" ).appendchild( s ); }
i'm made simple test. create html page content:
<!doctype html> <html> <head> <title>test</title> <script type="text/javascript"> function getjavascript() { var s = document.createelement( "script" ); s.type = "text/javascript"; s.async = true; s.src = "get_javascript.js"; document.getelementbyid( "javascriptholder" ).appendchild( s ); } </script> </head> <body> <button type="button" onclick="getjavascript();">get javascript</button> <div id="javascriptholder"></div> </body> </html>
and file get_javascript.js
this:
alert( 'i javascript' );
and when click on button works charm.
Comments
Post a Comment