<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CAIH.org &#187; modularjs</title>
	<atom:link href="http://caih.org/category/open-source-software/modularjs/feed/" rel="self" type="application/rss+xml" />
	<link>http://caih.org</link>
	<description>Movies, life and python</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Loading javascript: execScript and testing</title>
		<link>http://caih.org/open-source-software/loading-javascript-execscript-and-testing/</link>
		<comments>http://caih.org/open-source-software/loading-javascript-execscript-and-testing/#comments</comments>
		<pubDate>Thu, 14 May 2009 12:14:33 +0000</pubDate>
		<dc:creator>César Izurieta</dc:creator>
				<category><![CDATA[Open Source Software]]></category>
		<category><![CDATA[modularjs]]></category>

		<guid isPermaLink="false">http://caih.org/?p=113</guid>
		<description><![CDATA[After a lot of time of searching what is the best way to synchronously load javascript from javascript I finally found a way to do it that works in almost any browser. The hardest part of it all is not loading and eval&#8217;ing the script, but allowing the script to set new variables in the [...]


Related posts:<ol><li><a href='http://caih.org/open-source-software/tri-state-checkbox-in-javascript/' rel='bookmark' title='Permanent Link: Tri-state checkbox in JavaScript'>Tri-state checkbox in JavaScript</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>After a lot of time of searching what is the best way to synchronously load javascript from javascript I finally found a way to do it that works in almost any browser. The hardest part of it all is not loading and eval&#8217;ing the script, but allowing the script to set new variables in the global scope.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">execScript</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; window.<span style="color: #660066;">execScript</span><span style="color: #009900;">&#40;</span>contents<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">with</span> <span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; window.<span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>contents<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>This is what I&#8217;m finally using on my <a href="http://modularjs.googlecode.com">open source modular javascript project</a>. To test it out I also found <a href="http://browsershots.com" rel="nofollow">this website</a> very useful. What I did was to create a test page that loaded a script, declared some global variables in different ways:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> someVariable <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
someVariableWithoutVar <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;var someVariableWithEval = true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;someVariableWithEvalWithoutVar = true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Then loaded the website through browsershots and the results came in a few minutes. The <a href="http://browsershots.org/http://modularjs.googlecode.com/svn/trunk/test/index.html" target="_blank">tests passed</a> on a lot of browsers including all versions I could test of Firefox, Opera, MSIE, Safari, with some minor exceptions: Safari 3.2.1 on Windows XP and OS X 10.5, Opera 7.54 on Windows XP, Konqueror 4.2 on Ubuntu 9.04.</p>
<p>The case with Safari 3.2.1 is that it cannot set global variables declared with <em>var</em> inside a eval&#8217;ed script. There&#8217;s a newer 3.2.3 version that I would like to test on to see if there is any difference. As for Opera, it&#8217;s an old version that nobody is probably using anymore. But for Konqueror I really don&#8217;t know the source of the error.</p>
<p>Take the <a href="http://modularjs.googlecode.com/svn/trunk/test/index.html">test</a> yourself. If you would like to see how the tests were done take a look at the sources <a href="http://code.google.com/p/modularjs/source/browse/#svn/trunk/test">here</a>.</p>
<p><strong>Update:</strong> The test suite now passes in almost every browser with the exception of some older versions of opera <img src='http://caih.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>


<p>Related posts:<ol><li><a href='http://caih.org/open-source-software/tri-state-checkbox-in-javascript/' rel='bookmark' title='Permanent Link: Tri-state checkbox in JavaScript'>Tri-state checkbox in JavaScript</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://caih.org/open-source-software/loading-javascript-execscript-and-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loading javascript: &lt;script&gt; vs. ajax vs. both</title>
		<link>http://caih.org/open-source-software/modularjs/loading-javascript-script-vs-ajax-vs-both/</link>
		<comments>http://caih.org/open-source-software/modularjs/loading-javascript-script-vs-ajax-vs-both/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 01:46:41 +0000</pubDate>
		<dc:creator>César Izurieta</dc:creator>
				<category><![CDATA[modularjs]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[modules]]></category>

		<guid isPermaLink="false">http://caih.org/?p=32</guid>
		<description><![CDATA[On my previous post I looked at how some frameworks handle the multimodule approach to separate javascript code. Since then I&#8217;ve been playing with different ways of loading scripts online vs. doing it offline. After doing some research on the subject I found this website where they show three different approaches to load javascript files. [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>On my <a href="/open-source-software/modularjs/loading-javascript-code-dynamically/">previous post</a> I looked at how some frameworks handle the multimodule approach to separate javascript code. Since then I&#8217;ve been playing with different ways of loading scripts online vs. doing it offline.</p>
<p>After doing some research on the subject I found this website where they show <a title="Dynamically Loading external JavaScript file" href="http://www.web2coder.com/website-design/dynamically-loading-external-javascript-file-3" target="_blank">three different approaches</a> to load javascript files. I did some testing and here are some results. The three methods to load the javascript tested were:</p>
<p>Inserting a script tag with a src into the head of the document:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> loadScriptTag <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> head <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;head&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> script <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;script&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;text/javascript&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; script.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> src<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; head.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>script<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>Inserting a script tag with code fetched using ajax into the head of the document:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> loadScriptTagWithAjax <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> response <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #660066;">Request</span><span style="color: #009900;">&#40;</span>src<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; method<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;get&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; evalJS<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; asynchronous<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> head <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;head&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> script <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;script&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;text/javascript&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; script.<span style="color: #660066;">text</span> <span style="color: #339933;">=</span> response.<span style="color: #660066;">transport</span>.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; head.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>script<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>Using eval to execute the code fetched using ajax:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> loadScriptAjax <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> response <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #660066;">Request</span><span style="color: #009900;">&#40;</span>src<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; method<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;get&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; evalJS<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; asynchronous<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>response.<span style="color: #660066;">transport</span>.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>The test code was the following:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> log <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">new</span> Insertion.<span style="color: #660066;">Bottom</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">body</span><span style="color: #339933;">,</span> m <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;br /&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;h1&gt;loadScriptTag&lt;/h1&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Before&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
loadScriptTag<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test-tag.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;After&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;h1&gt;loadScriptTagWithAjax&lt;/h1&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Before&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
loadScriptTagWithAjax<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test-tag-with-ajax.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;After&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;h1&gt;loadScriptAjax&lt;/h1&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Before&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
loadScriptAjax<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test-ajax.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;After&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>This files were loaded using a local Apache server. The results were the following:</p>
<h2>Firefox 3.1 Beta 2 on Mac</h2>
<blockquote><p>
<strong>loadScriptTag</strong><br />
Before<br />
After</p>
<p><strong>loadScriptTagWithAjax</strong><br />
Before<br />
Test loadScriptTag<br />
Test loadScriptTagWithAjax<br />
After</p>
<p><strong>loadScriptAjax</strong><br />
Before<br />
Test loadScriptAjax<br />
After
</p></blockquote>
<p>It seems that firefox executes the script with src just before the script with text everytime. Any way it is asynchronous.</p>
<h2>Safari 3.2 on Mac</h2>
<blockquote><p>
<strong>loadScriptTag</strong><br />
Before<br />
After</p>
<p><strong>loadScriptTagWithAjax</strong><br />
Before<br />
Test loadScriptTag<br />
Test loadScriptTagWithAjax<br />
After</p>
<p><strong>loadScriptAjax</strong><br />
Before<br />
Test loadScriptAjax<br />
After
</p></blockquote>
<p>Safari works just like firefox.</p>
<h2>Opera 10 Alpha on Mac</h2>
<blockquote><p>
<strong>loadScriptTag</strong><br />
Before<br />
After</p>
<p><strong>loadScriptTagWithAjax</strong><br />
Before<br />
After</p>
<p><strong>loadScriptAjax</strong><br />
Before<br />
Test loadScriptAjax<br />
After<br />
Test loadScriptTag<br />
Test loadScriptTagWithAjax
</p></blockquote>
<p>Opera executes the script tags when all the code is done running.</p>
<h2>IE 6.0 on Mac using ies4osx</h2>
<blockquote><p>
<strong>loadScriptTag</strong><br />
Before<br />
After</p>
<p><strong>loadScriptTagWithAjax</strong><br />
Before<br />
Test loadScriptTagWithAjax<br />
After</p>
<p><strong>loadScriptAjax</strong><br />
Before<br />
Test loadScriptAjax<br />
After<br />
Test loadScriptTag
</p></blockquote>
<p>IE executes the script tag with src at the end but the script tag with content in the right place.</p>
<p>Overall the only common result is using ajax with eval. This is my choice for <a title="modularjs" href="http://modularjs.googlecode.com" target="_blank">modularjs</a>. </p>
<p><a href='http://caih.org/wp-content/uploads/2009/01/js-loading-test.zip'>Grab here the sources for these tests</a></p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://caih.org/open-source-software/modularjs/loading-javascript-script-vs-ajax-vs-both/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loading javascript code dynamically</title>
		<link>http://caih.org/open-source-software/modularjs/loading-javascript-code-dynamically/</link>
		<comments>http://caih.org/open-source-software/modularjs/loading-javascript-code-dynamically/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 23:08:42 +0000</pubDate>
		<dc:creator>César Izurieta</dc:creator>
				<category><![CDATA[modularjs]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[modules]]></category>

		<guid isPermaLink="false">http://caih.org/?p=19</guid>
		<description><![CDATA[When I&#8217;m developing small or medium or large javascript projects I end up with the same problem all the time: code grows and it starts getting a mess if you don&#8217;t do something about it. The only way out is separating your code into different modules which leads to the only conclusion of separating it [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>When I&#8217;m developing small or medium or large javascript projects I end up with the same problem all the time: code grows and it starts getting a mess if you don&#8217;t do something about it. The only way out is separating your code into different modules which leads to the only conclusion of separating it on different files.</p>
<p>Most javascripts frameworks use different approaches to this. Some use an offline builder to create a single javascript file they distribute. Some other use a online dependency loading through different methods and some others a combination of both. Here&#8217;s a small list of frameworks and what they do:</p>
<h2>YUI Loader Utility</h2>
<p>If you plan to use the <a title="YUI Loader Utility" href="http://developer.yahoo.com/yui/yuiloader/" target="_blank">YUI Loader Utility</a>, it means that you&#8217;ll have to add it to your page, in other words 9.4kb more, not too bad. You can load any javascript file. It also supports rolled up files, this means that you can have all the source files &#8220;rolled up&#8221; on one file and the loader works exactly the same way as if you were using separate files. This is an online only loader. It looks like this:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> loader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">YUILoader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; require<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;colorpicker&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;treeview&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
loader.<span style="color: #660066;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>For custom modules it looks like this:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">loader.<span style="color: #660066;">addModule</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;json&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;js&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; fullpath<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.json.org/json2.js&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; varName<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;JSON&quot;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h2>Dojo Toolkit</h2>
<p>This choice means that you&#8217;ll have to build over dojo and use dojo&#8217;s require and provide mechanisms to request and provide modules online. It also has a ant and rhino based build system that can pack offline all modules in one file. The base dojo script takes ~100kb compressed which is quite large. I&#8217;m not really sure if the build system would work without the base dojo.js file. It looks like this:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">dojo.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dijit.form.Button&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>And the module file, in this case by default at dijit/form/Button.js, should start with:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">dojo.<span style="color: #660066;">provides</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dijit.form.Button&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h2>Prototoype</h2>
<p>The Prototype <a title="PrototypeJS build system" href="http://www.prototypejs.org/contribute" target="_blank">build system</a> uses Rake. This approach has the benefit of having multiple source javascript files and through a build system combine them on a single file. The downside is that you cannot choose at runtime which modules to load or not making it a all-or-nothing package. Also while developing you&#8217;ll need a ruby server to be able to test your code without compiling it. The code you need looks like this:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#996600;">'lang/class.js'</span>, <span style="color:#996600;">'lang/object.js'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></div>
<p>Note that it is not javascript but ruby code.</p>
<h2>Mochikit</h2>
<p>The <a title="Roll Your Own MochiKit" href="http://trac.mochikit.com/wiki/RollYourOwn" target="_blank">Mochikit build system</a> is based on python and rhino. It&#8217;s an offline packer and for development you&#8217;ll have to add all the scripts you are developing and dependencies on the right order. It has some sort of validation of import like this:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MochiKit.<span style="color: #660066;">Base</span>._module<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Format'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'1.5'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Base'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
...<br />
<span style="color: #660066;">MochiKit</span>.<span style="color: #660066;">Base</span>._exportSymbols<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> MochiKit.<span style="color: #660066;">Format</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>In the next article I&#8217;ll write about the internal methods used to load javascript files online, their advantages and disadvantages as well as <a title="modularjs" href="http://modularjs.googlecode.com" target="_blank">my own way of doing it</a>.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://caih.org/open-source-software/modularjs/loading-javascript-code-dynamically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
