{"id":33,"date":"2005-11-11T23:19:00","date_gmt":"2005-11-11T23:19:00","guid":{"rendered":"http:\/\/blog.trungson.com\/?p=33"},"modified":"2005-11-11T23:19:00","modified_gmt":"2005-11-11T23:19:00","slug":"how-to-insert-a-block-of-javascript-code-to-a-html-page","status":"publish","type":"post","link":"http:\/\/blog.trungson.com\/?p=33","title":{"rendered":"How to insert a block of javascript code to a html page&#8230;"},"content":{"rendered":"<p>Issue: innerHTML simply writes HTML as a string; it doesn&#8217;t call the javascript interpreter to parse and execute any scripts that are part of the string<\/p>\n<p>Reference: http:\/\/www.webmasterworld.com\/forum91\/2912.htm<\/p>\n<p>Solution: manually find these scripts inside the text string and append them to the document<br \/>\n<textarea name=\"code\" class=\"javascript\"><br \/>\n$vPat = new RegExp(&#8216;<script .+>([^<]+)<\/script>','g');<br \/>\nRegExp.lastIndex = 0; \/\/ making sure<br \/>\nwhile (($vResult=$vPat.exec($vTxt))!=null) {<br \/>\n for (var i=1;i<$vResult.length;i++) {\n  var js = document.createElement(\"script\");\n  js.setAttribute(\"type\",\"text\/javascript\");\n  js.text = $vResult[i];\n  document.getElementById($vDivID).appendChild(js);\n } \/\/ rof\n} \/\/ rof\n<\/textarea><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Issue: innerHTML simply writes HTML as a string; it doesn&#8217;t call the javascript interpreter to parse and execute any scripts that are part of the string Reference: http:\/\/www.webmasterworld.com\/forum91\/2912.htm Solution: manually find these scripts inside the text string and append them to the document $vPat = new RegExp(&#8216;&#8217;,&#8217;g&#8217;); RegExp.lastIndex = 0; \/\/ making sure while (($vResult=$vPat.exec($vTxt))!=null) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/33"}],"collection":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33"}],"version-history":[{"count":0,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/33\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}