<?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; ajax</title>
	<atom:link href="http://caih.org/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://caih.org</link>
	<description>Movies, life and python</description>
	<lastBuildDate>Mon, 11 Oct 2010 03:53:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<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>
	</channel>
</rss>

