I agree with Kyle Simpson (@Getify, creator of LABjs) in that we shouldn't need 
script loaders - hence his fight to get async=true implemented as part of the 
spec - but at this stage we don't have a whole lot of alternatives.

But saying that, I'm currently working on an AJAX based script loader at the 
moment which doesn't require hacks and browser sniffs/inference to parallel 
load scripts and to keep execution order - just plain JavaScript. The only big 
downside is that the scripts being loaded need to be on the same server as they 
are grabbed via AJAX (unlike the standard script insert method) but I don't 
mind because I like to load scripts from my own server anyway. 

Note: even libraries like jQuery, which are hosted on Google's CDN's I prefer 
to host on my own servers. This I decided on after the Google servers had a 
problem last year where their server was returning the jQuery script but only 
returning half of it. So even though I was using a fallback for when jQuery was 
undefined (so if it was undefined then it would load a local copy) this was 
failing because jQuery wasn't undefined, but it was still broken. This lasted 
for a few days and in that time nearly all of my clients sites stopped working 
so I had to go through and instead reference local copies of jQuery.

So for that reason alone I think a standard AJAX script loader is fine to use. 
BUT if you do have a need to load a remote script then libraries like LABjs are 
there to help you.

M.

-- 
Mark McDonnell
On Tuesday, 8 March 2011 at 19:43, Baris Bikmaz wrote: 
> I think this is a general problem with JavaScript. We shouldn't use libraries 
> to load JavaScript dynamically. JavaScript should do this by itself like 
> other scripting languages. But the concept of loading scripts must be 
> different, like should I load the script asynchron or synchron, should the 
> script be excecuted while loading a.s.o. There was some interesting posts to 
> this by Nicholas Zakas. A must-read I think. 
> 
> - http://www.nczonline.net/blog/2010/12/21/thoughts-on-script-loaders/
> - 
> http://www.nczonline.net/blog/2011/02/14/separating-javascript-download-and-execution/
> 
> 2011/3/8 Nick Morgan <skilldr...@gmail.com>
> >  Sorry, I've just realised this doesn't work, because it's not waiting for 
> > the script to load. Like Diego said, if you're going to do it this way then 
> > fire the callback on the onload event of the script.
> > 
> > 
> > On 8 March 2011 12:12, Nick Morgan <skilldr...@gmail.com> wrote:
> > >  A really basic fix would be:
> > > 
> > > function loadScript(url, callback) {
> > > 
> > > var script = document.createElement('script');
> > > script.src= url + "?r=" + Math.floor(Math.random()*10000);
> > > script.setAttribute("charset", "UTF-8")
> > > document.querySelector('head').appendChild(script);
> > > if (typeof callback == 'function') {
> > >  callback();
> > > }
> > >  }
> > > 
> > > loadScript("script-1.js", function () {
> > > loadScript("script-2.js", function () {
> > > loadScript("script-3.js")
> > > });
> > > });
> > > 
> > > (untested but you get the idea)
> > > 
> > >  On 5 March 2011 17:46, Jarek Foksa <ja...@kiwi-themes.com> wrote:
> > > >  Let's say that I have four scripts: init.js, script-1.js, script-2.js
> > > >  and script-3.js. Only init.js is declared in XHTML file, the other
> > > >  three scripts are loaded from init.js. The code for each file is as
> > > >  follows:
> > > > 
> > > >  init.js
> > > >  ----------------------------------------------------
> > > >  function loadScript(url) {
> > > > var script = document.createElement('script');
> > > > script.src= url + "?r=" + Math.floor(Math.random()*10000);
> > > > script.setAttribute("charset", "UTF-8")
> > > > document.querySelector('head').appendChild(script);
> > > >  }
> > > >  loadScript("script-1.js");
> > > >  loadScript("script-2.js");
> > > >  loadScript("script-3.js");
> > > > 
> > > >  script-1.js:
> > > >  ----------------------------------------------------
> > > >  console.log("Executing script-1.js");
> > > > 
> > > >  script-2.js
> > > >  ----------------------------------------------------
> > > >  console.log("Executing script-2.js");
> > > > 
> > > >  script-3.js
> > > >  ----------------------------------------------------
> > > >  console.log("Executing script-3.js");
> > > > 
> > > > 
> > > >  I'm always getting correct results in Google Chrome, Firefox and Opera:
> > > > > Executing script-1.js
> > > > > Executing script-2.js
> > > > > Executing script-3.js
> > > > 
> > > >  But for some reason the scripts are loaded in random order by Safari:
> > > > > Executing script-3.js
> > > > > Executing script-1.js
> > > > > Executing script-2.js
> > > > 
> > > >  It looks like loadScript() function runs asynchronously in Safari and
> > > >  synchronously in all other browsers. Why? Do you know a more reliable
> > > >  way for dynamically loading script files? So far I have stumbled upon
> > > >  RequireJS library, though it seems to be an overkill for my tiny
> > > >  projects.
> > > > 
> > > >  --
> > > >  To view archived discussions from the original JSMentors Mailman list: 
> > > > http://www.mail-archive.com/jsmentors@jsmentors.com/
> > > > 
> > > >  To search via a non-Google archive, visit here: 
> > > > http://www.mail-archive.com/jsmentors@googlegroups.com/
> > > > 
> > > >  To unsubscribe from this group, send email to
> > > > jsmentors+unsubscr...@googlegroups.com
> > > > 
> > > 
> > > 
> > > -- 
> > > Nick Morgan
> > > http://skilldrick.co.uk
> > > @skilldrick
> > > 
> > > 
> > 
> > 
> > -- 
> > Nick Morgan
> > http://skilldrick.co.uk
> > @skilldrick
> > 
> >  -- 
> >  To view archived discussions from the original JSMentors Mailman list: 
> > http://www.mail-archive.com/jsmentors@jsmentors.com/
> > 
> >  To search via a non-Google archive, visit here: 
> > http://www.mail-archive.com/jsmentors@googlegroups.com/
> > 
> >  To unsubscribe from this group, send email to
> > jsmentors+unsubscr...@googlegroups.com
> > 
>  -- 
>  To view archived discussions from the original JSMentors Mailman list: 
> http://www.mail-archive.com/jsmentors@jsmentors.com/
> 
>  To search via a non-Google archive, visit here: 
> http://www.mail-archive.com/jsmentors@googlegroups.com/
> 
>  To unsubscribe from this group, send email to
> jsmentors+unsubscr...@googlegroups.com
> 

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to