<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: When is a JavaScript include ready?</title>
	<atom:link href="http://www.phpied.com/javascript-include-ready/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpied.com/javascript-include-ready/</link>
	<description>Stoyan&#039;s blog about &#60;a href=&#34;/category/xhtml&#34; class=&#34;tag-minor&#34;&#62;(x)html(5)&#60;/a&#62;, &#60;a href=&#34;/category/ajax&#34; class=&#34;tag-major&#34;&#62;ajax&#60;/a&#62;, &#60;a href=&#34;/category/bookmarklets&#34; class=&#34;tag-major&#34;&#62;bookmarklets&#60;/a&#62;, &#60;a href=&#34;/category/browsers&#34; class=&#34;tag-minor&#34;&#62;browsers&#60;/a&#62;, &#60;a href=&#34;/category/css&#34; class=&#34;tag-normal&#34;&#62;css&#60;/a&#62;, &#60;a href=&#34;/category/firebug&#34; class=&#34;tag-minor&#34;&#62;firebug&#60;/a&#62;, &#60;a href=&#34;/category/javascript&#34; class=&#34;tag-numero-uno&#34;&#62;javascript&#60;/a&#62;, &#60;a href=&#34;/category/json&#34; class=&#34;tag-normal&#34;&#62;json&#60;/a&#62;, &#60;a href=&#34;/category/mdb2&#34; class=&#34;tag-minor&#34;&#62;mdb2&#60;/a&#62;, &#60;a href=&#34;/category/mysql&#34; class=&#34;tag-normal&#34;&#62;mysql&#60;/a&#62;, &#60;a href=&#34;/category/pear&#34; class=&#34;tag-numero-uno&#34;&#62;pear&#60;/a&#62;, &#60;a href=&#34;/category/performance&#34; class=&#34;tag-major&#34;&#62;performance&#60;/a&#62;, &#60;a href=&#34;/category/php&#34; class=&#34;tag-numero-uno&#34;&#62;php&#60;/a&#62;, &#60;a href=&#34;/category/phpbb&#34; class=&#34;tag-major&#34;&#62;phpbb&#60;/a&#62;, &#60;a href=&#34;/category/tools&#34; class=&#34;tag-normal&#34;&#62;tools&#60;/a&#62;, &#60;a href=&#34;/category/yslow&#34; class=&#34;tag-minor&#34;&#62;yslow&#60;/a&#62;, &#60;a href=&#34;/category/yui&#34; class=&#34;tag-normal&#34;&#62;yui&#60;/a&#62;, &#60;a href=&#34;/category/writing&#34; class=&#34;tag-minor&#34;&#62;writing&#60;/a&#62;, &#60;a href=&#34;/category/music&#34; class=&#34;tag-major&#34;&#62;music&#60;/a&#62;,... &#60;a href=&#34;/category/life-and-everything&#34; class=&#34;tag-normal&#34;&#62;life and everything&#60;/a&#62;.</description>
	<lastBuildDate>Sat, 11 Feb 2012 14:07:43 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
	<item>
		<title>By: 加载外部文件 &#171; Kejun&#8217;s Blog</title>
		<link>http://www.phpied.com/javascript-include-ready/#comment-70968</link>
		<dc:creator>加载外部文件 &#171; Kejun&#8217;s Blog</dc:creator>
		<pubDate>Mon, 01 Jun 2009 15:12:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/javascript-include-ready/#comment-70968</guid>
		<description>[...] 利用onReadyStateChange和onLoad的思路 http://www.phpied.com/javascript-include-ready/http://unixpapa.com/js/dyna.html [...]</description>
		<content:encoded><![CDATA[<p>[...] 利用onReadyStateChange和onLoad的思路 <a href="http://www.phpied.com/javascript-include-ready/http://unixpapa.com/js/dyna.html" rel="nofollow">http://www.phpied.com/javascript-include-ready/http://unixpapa.com/js/dyna.html</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: phpied.com &#187; Blog Archive &#187; My online footprint lately</title>
		<link>http://www.phpied.com/javascript-include-ready/#comment-69774</link>
		<dc:creator>phpied.com &#187; Blog Archive &#187; My online footprint lately</dc:creator>
		<pubDate>Fri, 25 Jul 2008 04:37:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/javascript-include-ready/#comment-69774</guid>
		<description>[...] YUI Blog just published my first article, I&#039;m so proud. It&#039;s about loading JavaScript in non-blocking fashion, because JavaScripts, they, you know, like, block downloads. Luckily, there&#039;s an easy fix - DOM includes, which I&#039;ve previously discussed, discussed and discussed. [...]</description>
		<content:encoded><![CDATA[<p>[...] YUI Blog just published my first article, I&#8217;m so proud. It&#8217;s about loading JavaScript in non-blocking fashion, because JavaScripts, they, you know, like, block downloads. Luckily, there&#8217;s an easy fix &#8211; DOM includes, which I&#8217;ve previously discussed, discussed and discussed. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shimon doodkin</title>
		<link>http://www.phpied.com/javascript-include-ready/#comment-50168</link>
		<dc:creator>shimon doodkin</dc:creator>
		<pubDate>Mon, 12 Mar 2007 09:59:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/javascript-include-ready/#comment-50168</guid>
		<description>what when a script contains already parameters
like myscript.js.php?id=5
as previous i would make myscript.js.php?id=5?r=465465465
and the server receives  id=&quot;5?r=465465465&quot;
instead of id=5


notice the: 
(script_filename.indexOf(&quot;?&quot;)?&quot;&amp;&quot;:&quot;?&quot;)

simpler:
if(script_filename.indexOf(&quot;?&quot;))
 script_filename +=&quot;&amp;&quot;
else
 script_filename +=&quot;?&quot;


a trick lernt from yahoo UI javascript
when appending p-arameters to a url


function include_rpc(script_filename) {

    script_filename += (script_filename.indexOf(&quot;?&quot;)?&quot;&amp;&quot;:&quot;?&quot;) + Math.random(0, 1000) + &#039;=&#039; + Math.random(0, 1000);
    var html_doc = document.getElementsByTagName(&#039;head&#039;).item(0);
    var js = document.createElement(&#039;script&#039;);
    js.setAttribute(&#039;language&#039;, &#039;javascript&#039;);
    js.setAttribute(&#039;type&#039;, &#039;text/javascript&#039;);
    js.setAttribute(&#039;src&#039;, script_filename);
    html_doc.appendChild(js);
    return false;

}</description>
		<content:encoded><![CDATA[<p>what when a script contains already parameters<br />
like myscript.js.php?id=5<br />
as previous i would make myscript.js.php?id=5?r=465465465<br />
and the server receives  id=&#8221;5?r=465465465&#8243;<br />
instead of id=5</p>
<p>notice the:<br />
(script_filename.indexOf(&#8220;?&#8221;)?&#8221;&amp;&#8221;:&#8221;?&#8221;)</p>
<p>simpler:<br />
if(script_filename.indexOf(&#8220;?&#8221;))<br />
 script_filename +=&#8221;&amp;&#8221;<br />
else<br />
 script_filename +=&#8221;?&#8221;</p>
<p>a trick lernt from yahoo UI javascript<br />
when appending p-arameters to a url</p>
<p>function include_rpc(script_filename) {</p>
<p>    script_filename += (script_filename.indexOf(&#8220;?&#8221;)?&#8221;&amp;&#8221;:&#8221;?&#8221;) + Math.random(0, 1000) + &#8216;=&#8217; + Math.random(0, 1000);<br />
    var html_doc = document.getElementsByTagName(&#8216;head&#8217;).item(0);<br />
    var js = document.createElement(&#8216;script&#8217;);<br />
    js.setAttribute(&#8216;language&#8217;, &#8216;javascript&#8217;);<br />
    js.setAttribute(&#8216;type&#8217;, &#8216;text/javascript&#8217;);<br />
    js.setAttribute(&#8216;src&#8217;, script_filename);<br />
    html_doc.appendChild(js);<br />
    return false;</p>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeremy Nicoll</title>
		<link>http://www.phpied.com/javascript-include-ready/#comment-48361</link>
		<dc:creator>Jeremy Nicoll</dc:creator>
		<pubDate>Fri, 05 Jan 2007 05:06:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/javascript-include-ready/#comment-48361</guid>
		<description>The problem is that Safari does not work with any of the load event handlers that I have tried.  I am attempting to write a script that will load each required JS file in succession and call any code that relies on these files once they are loaded, but Safari will not load any but the first one as I am relying on the load event handlers.   Really frustrating, it is.</description>
		<content:encoded><![CDATA[<p>The problem is that Safari does not work with any of the load event handlers that I have tried.  I am attempting to write a script that will load each required JS file in succession and call any code that relies on these files once they are loaded, but Safari will not load any but the first one as I am relying on the load event handlers.   Really frustrating, it is.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicholas</title>
		<link>http://www.phpied.com/javascript-include-ready/#comment-40049</link>
		<dc:creator>Nicholas</dc:creator>
		<pubDate>Sat, 25 Nov 2006 02:13:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/javascript-include-ready/#comment-40049</guid>
		<description>Hi, I just created a dynamic include script using modified code from this blog. It works by creating an array of files that are included, and also a list of files *the have loaded.*

After calling all the necessary includes, the user calls &quot;waitToLoadFiles(postLoadFunc)&quot; which will periodically check to see if the list of loaded files matches the list of included files. Once the list matches, &quot;postLoadFunc&quot; is executed.

This script is tested in FF1.5.0.8 and works great!! Fails to work in IE5.1, though, and I can&#039;t understand why. It seems to load some files, but then it stops. I&#039;m thinking it has something to do with the include_once function being called again within included files. If anyone can figure it out, let me know! 

velocityidp at gmail dot com

&lt;pre style=&quot;font-weight: normal&quot;&gt;
var js;
var html_doc = document.getElementsByTagName(&#039;head&#039;)[0];
var loaded_files = new Array();

function include_js(js_file) {
       js = document.createElement(&#039;script&#039;);
       js.setAttribute(&#039;type&#039;, &#039;text/javascript&#039;);
       js.setAttribute(&#039;src&#039;, js_file);
       js.file = js_file;
       html_doc.appendChild(js);

       js.onreadystatechange = function () {
               if(this.readyState == &#039;complete&#039;)
                       loaded_files[loaded_files.length] = this.file;
       }

       js.onload = function () {
         loaded_files[loaded_files.length] = this.file;
       }
       return false;
}

var css;
function include_css(css_file) {
       css = document.createElement(&#039;link&#039;);
       css.setAttribute(&#039;rel&#039;, &#039;stylesheet&#039;);
       css.setAttribute(&#039;type&#039;, &#039;text/css&#039;);
       css.setAttribute(&#039;href&#039;, css_file);
       css.file = css_file;
       html_doc.appendChild(css);

       // alert state change
       css.onreadystatechange = function () {
               if (this.readyState == &#039;complete&#039;)
                       loaded_files[loaded_files.length] = this.file;
       }
       css.onload = function () {
               loaded_files[loaded_files.length] = this.file;
       }


       return false;
}

var included_files = new Array();
function include_once(file, type) {
       if (!in_array(file, included_files)) {
       included_files[included_files.length] = file;

       switch(type){
               case &#039;js&#039;:
                       include_js(file);
                       break;
               case &#039;css&#039;:
                       include_css(file);
               default:
                       include_js(file);
                       break;
               }
       }
}

function in_array(needle, haystack) {
       for (var i = 0; i &lt; haystack.length; i++) {
               if (haystack[i] == needle) {
                       return true;
               }
       }
       return false;
}

var granularity = 1000; //ms
function waitToLoadFiles(postLoadFunc){
       if(areAllFilesLoaded())
               setTimeout(postLoadFunc+&quot;()&quot;, 1);
       else{
               alert(&quot;Not loaded
&quot;+loaded_files.length+&quot;/&quot;+included_files.length+&quot;\n\n&quot;+loaded_files.toString
());
               setTimeout(&quot;waitToLoadFiles(&#039;&quot;+postLoadFunc+&quot;&#039;)&quot;,
granularity);
       }
}


function areAllFilesLoaded(){
       if(included_files.length == loaded_files.length)
               return true;
       else
               return false;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi, I just created a dynamic include script using modified code from this blog. It works by creating an array of files that are included, and also a list of files *the have loaded.*</p>
<p>After calling all the necessary includes, the user calls &#8220;waitToLoadFiles(postLoadFunc)&#8221; which will periodically check to see if the list of loaded files matches the list of included files. Once the list matches, &#8220;postLoadFunc&#8221; is executed.</p>
<p>This script is tested in FF1.5.0.8 and works great!! Fails to work in IE5.1, though, and I can&#8217;t understand why. It seems to load some files, but then it stops. I&#8217;m thinking it has something to do with the include_once function being called again within included files. If anyone can figure it out, let me know! </p>
<p>velocityidp at gmail dot com</p>
<pre style="font-weight: normal">
var js;
var html_doc = document.getElementsByTagName('head')[0];
var loaded_files = new Array();

function include_js(js_file) {
       js = document.createElement('script');
       js.setAttribute('type', 'text/javascript');
       js.setAttribute('src', js_file);
       js.file = js_file;
       html_doc.appendChild(js);

       js.onreadystatechange = function () {
               if(this.readyState == 'complete')
                       loaded_files[loaded_files.length] = this.file;
       }

       js.onload = function () {
         loaded_files[loaded_files.length] = this.file;
       }
       return false;
}

var css;
function include_css(css_file) {
       css = document.createElement('link');
       css.setAttribute('rel', 'stylesheet');
       css.setAttribute('type', 'text/css');
       css.setAttribute('href', css_file);
       css.file = css_file;
       html_doc.appendChild(css);

       // alert state change
       css.onreadystatechange = function () {
               if (this.readyState == 'complete')
                       loaded_files[loaded_files.length] = this.file;
       }
       css.onload = function () {
               loaded_files[loaded_files.length] = this.file;
       }

       return false;
}

var included_files = new Array();
function include_once(file, type) {
       if (!in_array(file, included_files)) {
       included_files[included_files.length] = file;

       switch(type){
               case 'js':
                       include_js(file);
                       break;
               case 'css':
                       include_css(file);
               default:
                       include_js(file);
                       break;
               }
       }
}

function in_array(needle, haystack) {
       for (var i = 0; i &lt; haystack.length; i++) {
               if (haystack[i] == needle) {
                       return true;
               }
       }
       return false;
}

var granularity = 1000; //ms
function waitToLoadFiles(postLoadFunc){
       if(areAllFilesLoaded())
               setTimeout(postLoadFunc+&quot;()&quot;, 1);
       else{
               alert(&quot;Not loaded
&quot;+loaded_files.length+&quot;/&quot;+included_files.length+&quot;\n\n&quot;+loaded_files.toString
());
               setTimeout(&quot;waitToLoadFiles('&quot;+postLoadFunc+&quot;')&quot;,
granularity);
       }
}

function areAllFilesLoaded(){
       if(included_files.length == loaded_files.length)
               return true;
       else
               return false;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: &#187; Carga dinámica de scripts: una buena aproximación - Scriptia</title>
		<link>http://www.phpied.com/javascript-include-ready/#comment-33807</link>
		<dc:creator>&#187; Carga dinámica de scripts: una buena aproximación - Scriptia</dc:creator>
		<pubDate>Sat, 11 Nov 2006 17:41:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/javascript-include-ready/#comment-33807</guid>
		<description>[...] Stoyan Stefanov ha publicado recientemente en phpied.com un par de notas. Anda buscando (como muchos de nosotros) una buena manera de cargar scripts externos dinámicamente. El truco: crear un elemento script y asignarle un manejador para onreadystatechange (IE) o onload (Firefox). Desafortunadamente, esta aproximación deja fuera a Safari y Opera. Snif. Los enlaces: When is a JavaScript include ready? y JS includes - the saga continues….  Etiquetas en algún lugar, include, carga dinámica [...]</description>
		<content:encoded><![CDATA[<p>[...] Stoyan Stefanov ha publicado recientemente en phpied.com un par de notas. Anda buscando (como muchos de nosotros) una buena manera de cargar scripts externos dinámicamente. El truco: crear un elemento script y asignarle un manejador para onreadystatechange (IE) o onload (Firefox). Desafortunadamente, esta aproximación deja fuera a Safari y Opera. Snif. Los enlaces: When is a JavaScript include ready? y JS includes &#8211; the saga continues….  Etiquetas en algún lugar, include, carga dinámica [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stoyan</title>
		<link>http://www.phpied.com/javascript-include-ready/#comment-22598</link>
		<dc:creator>Stoyan</dc:creator>
		<pubDate>Tue, 24 Oct 2006 19:59:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/javascript-include-ready/#comment-22598</guid>
		<description>Thanks, Björn, wicked! I&#039;ll try it out!</description>
		<content:encoded><![CDATA[<p>Thanks, Björn, wicked! I&#8217;ll try it out!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Björn Graf</title>
		<link>http://www.phpied.com/javascript-include-ready/#comment-22580</link>
		<dc:creator>Björn Graf</dc:creator>
		<pubDate>Tue, 24 Oct 2006 19:01:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/javascript-include-ready/#comment-22580</guid>
		<description>Using the load event on the SCRIPT object works in Firefox and Opera (and most likely in Safari, too).</description>
		<content:encoded><![CDATA[<p>Using the load event on the SCRIPT object works in Firefox and Opera (and most likely in Safari, too).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aaron Bassett</title>
		<link>http://www.phpied.com/javascript-include-ready/#comment-22184</link>
		<dc:creator>Aaron Bassett</dc:creator>
		<pubDate>Mon, 23 Oct 2006 21:32:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/javascript-include-ready/#comment-22184</guid>
		<description>Stoyan: Yeah I know there could be times when the methods I mentioned wont be an option, but then there will be times when you&#039;ll need to support more than 1 browser ;) 

Depending upon the make up of the external file you are calling you *might* not need to alter it at all. As long as there is a variable/etc being set towards the end of the file you can check for it.
Same as when you use Object detection to test for browser support 
if(document.getElementById) { doDomStuff(); }

I also had a thought after having another look at your code, I know if you set an images src attribute as a js file certain browsers will download and execute it like any other external js file (I cant quite remember which at the moment)
So it might be possible to use the Image object&#039;s onload event?

If I can escape my better half&#039;s calls to help around the flat tonight I might get a chance to test this but am thinking something along the lines of....

function loadExternal(url) {
	var loader = new Image();
	loader.onLoad = externalLoaded;
	loader.src = url;
}

function externalLoaded() {
	// do stuff
}</description>
		<content:encoded><![CDATA[<p>Stoyan: Yeah I know there could be times when the methods I mentioned wont be an option, but then there will be times when you&#8217;ll need to support more than 1 browser <img src='http://www.phpied.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  </p>
<p>Depending upon the make up of the external file you are calling you *might* not need to alter it at all. As long as there is a variable/etc being set towards the end of the file you can check for it.<br />
Same as when you use Object detection to test for browser support<br />
if(document.getElementById) { doDomStuff(); }</p>
<p>I also had a thought after having another look at your code, I know if you set an images src attribute as a js file certain browsers will download and execute it like any other external js file (I cant quite remember which at the moment)<br />
So it might be possible to use the Image object&#8217;s onload event?</p>
<p>If I can escape my better half&#8217;s calls to help around the flat tonight I might get a chance to test this but am thinking something along the lines of&#8230;.</p>
<p>function loadExternal(url) {<br />
	var loader = new Image();<br />
	loader.onLoad = externalLoaded;<br />
	loader.src = url;<br />
}</p>
<p>function externalLoaded() {<br />
	// do stuff<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: phpied.com &#187; Blog Archive &#187; Javascript includes - yet another way of RPC-ing</title>
		<link>http://www.phpied.com/javascript-include-ready/#comment-22136</link>
		<dc:creator>phpied.com &#187; Blog Archive &#187; Javascript includes - yet another way of RPC-ing</dc:creator>
		<pubDate>Mon, 23 Oct 2006 18:52:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/javascript-include-ready/#comment-22136</guid>
		<description>[...] 2006-10-23 update: In IE, I found a way to tell when the new script is done loading - described here. [...]</description>
		<content:encoded><![CDATA[<p>[...] 2006-10-23 update: In IE, I found a way to tell when the new script is done loading &#8211; described here. [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

