{"id":1966,"date":"2005-12-17T21:25:27","date_gmt":"2005-12-17T21:25:27","guid":{"rendered":"http:\/\/sunpig.com\/mt-entry-1966.html"},"modified":"2006-10-03T22:06:12","modified_gmt":"2006-10-03T22:06:12","slug":"dynamically-created-or-included-javascript","status":"publish","type":"post","link":"https:\/\/sunpig.com\/martin\/2005\/12\/17\/dynamically-created-or-included-javascript\/","title":{"rendered":"Dynamically created (or included) Javascript"},"content":{"rendered":"<p>Dynamically created page elements?  No problem.  Dynamically created <em>Javascript<\/em>?  As in, using <code>document.createElement()<\/code> to create a <code>&lt;script&gt;<\/code> element?  Sounds like black magic, and it&#8217;s not something I would have tried myself until I saw the article <a href=\"http:\/\/www.naarvoren.nl\/zijlijn\/nieuws\/2005\/12\/import_voor_javascript\">@import voor JavaScript<\/a> on <a href=\"http:\/\/www.naarvoren.nl\/\">Naar Voren<\/a> (in Dutch).  Basically, it&#8217;s a technique for giving Javascript the ability to include other script files &#8220;on demand,&#8221; much in the same way as PHP&#8217;s include() and include_once() functions.<\/p>\n<p>This technique has actually been around for a while:  a quick google showed me the article <a href=\"http:\/\/www.phpied.com\/javascript-include\/\">&#8220;Javascript includes &#8211; yet another way of RPC-ing&#8221;<\/a>, by Stoyan Stefanov from July of this year, which in turn points back to articles <a href=\"http:\/\/www.dotvoid.com\/view.php?id=13\">from<\/a> <a href=\"http:\/\/www.dhtmlcentral.com\/tutorials\/tutorials.asp?id=11\">2002<\/a>.  In addition to making the whole include() thing possible (a <em>fantastically<\/em> useful feature), using dynamically generated script also allows you to use make remote calls to other domains&#8211;something the HmlHttpRequest object forbids.  In fact, <a href=\"http:\/\/simon.incutio.com\/archive\/2005\/12\/16\/json\">Simon Willison was just talking about this<\/a> the other day, in the context of Yahoo!&#8217;s web service APIs now providing output in <a href=\"http:\/\/www.crockford.com\/JSON\/index.html\">JSON<\/a> format as well as traditional XML.<\/p>\n<p>It&#8217;s all coming together.  A bundle of key techniques that have been around for ages (<a href=\"http:\/\/www.onlinetools.org\/articles\/unobtrusivejavascript\/\">Unobtrusive javascript<\/a>, <a href=\"http:\/\/prototype.conio.net\/\">object-oriented javascript<\/a>, <a href=\"http:\/\/www.adaptivepath.com\/publications\/essays\/archives\/000385.php\">Ajax\/remote scripting<\/a>) have suddenly seen massive adoption and tremendous development.  Javascript has matured a great deal over the course of 2005, and is rapidly tuning into one of the cornerstones of modern web development.  It&#8217;s a very exciting time for the field.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dynamically created page elements? No problem. Dynamically created Javascript? As in, using document.createElement() to create a &lt;script&gt; element? Sounds like black magic, and it&#8217;s not something I would have tried myself until I saw the article @import voor JavaScript on Naar Voren (in Dutch). Basically, it&#8217;s a technique for giving Javascript the ability to include &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/sunpig.com\/martin\/2005\/12\/17\/dynamically-created-or-included-javascript\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Dynamically created (or included) Javascript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[1208,1207,1218,1215,1216,1197,1198,1205,1214,1206,1217,1210,1212],"class_list":["post-1966","post","type-post","status-publish","format-standard","hentry","category-second_best","tag-ajax","tag-api","tag-dynamic","tag-import","tag-include","tag-javascript","tag-js","tag-json","tag-naarvoren","tag-php","tag-remote","tag-xmlhttprequest","tag-yahoo"],"_links":{"self":[{"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/posts\/1966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/comments?post=1966"}],"version-history":[{"count":0,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/posts\/1966\/revisions"}],"wp:attachment":[{"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/media?parent=1966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/categories?post=1966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/tags?post=1966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}