No worries on being pedantic, Alex. If I'd been working 24 hours straight,
I'd be worried I might understand a critical detail too.

Yes, I do mean to have you concatenate all of jquery.js along with the
plugin and your code into a single large .js file, and have your loader.js
create a dynamic script element to load that single file (or a
packed/minified/gzipped version of it).

No, it won't cause you any problems to concatenate jquery.js along with your
other files. There's nothing special about jquery.js; it's just a .js file
like any other.

There is one problem you can run into when you concatenate files. If one
source file has a missing semicolon at the very end, then when you append
another file, the JS interpreter may combine statements in a way you didn't
intend.

This shouldn't be a problem for jquery.js and a typical plugin, but it you
want to be absolutely sure, simply add a ; between each source file. You can
make that part of a separator line if you want, perhaps like this:

;//--------------------------------------------------------

jquery.js goes here

;//--------------------------------------------------------

Plugin goes here

;//--------------------------------------------------------

Your script goes here

;//--------------------------------------------------------

That way you get a nice visual separation between the scripts, and the extra
semicolons won't hurt anything.

-Mike

> From: Alex Weber
> Sent: Tuesday, September 02, 2008 2:07 PM
> To: jQuery (English)
> Subject: [jQuery] Re: best techniques to optimize loading of 
> multiple libraries?
> 
> 
> Thanks again Mike!
> 
> I reckon I have it down to a 'T' now! :)
> 
> Just one doubt (sorry i've taken so much of your time already!):
> 
> When you say:
> "Concatenate all of your scripts into a single file."
> 
> Do you mean the entire jquery library as well?
> 
> Is this feasible?  Like, copy and paste the entire jquery 
> library (uncompressed), one plugin, and my custom code into 
> one huge .js file and then pack or minify it?
> And host it somewhere fast like cachefly?
> 
> I can see doing this for the plugin and my custom js but 
> jquery also seems asking for trouble no?
> 
> Or am i not getting it?  Did you mean dynamically append "loader.js"
> in the main document body, and then in loader.js have 3 
> separate <script src>? (wait does that even work?)  sorry its 
> late here ive been on a crazy reching on 24 hour work bender.... :S
> 
> Thanks again and sorry for being so pedantic!!
> 
> Alex
> 
> On Sep 2, 4:40 pm, "Michael Geary" <[EMAIL PROTECTED]> wrote:
> > > From: Alex Weber
> >
> > > So just to clear things up, you prefer using document.write() to 
> > > insert <script> tags instead of appending the elements to the DOM:
> >
> > >   var finan = document.createElement('script');
> > >           finan.type = 'text/javascript';
> > >                 finan.src = 
> > > 'http://www.mydomain.com/js/mts_finan.js';
> > >           head.appendChild(finan);
> >
> > > i guess its more efficient because you don't have to access the 
> > > document object, but any other particular reasons?
> >
> > No, that isn't the reason at all. It's because it makes the 
> code much 
> > simpler. You don't have to write any code to load the 
> scripts in their 
> > proper sequence; it happens automatically. <script> tags 
> are evaluated 
> > in the order they appear in the source code, and code you add with 
> > document.write is inserted immediately after the end of the 
> script tag 
> > that does the document.write.
> >
> > Dynamically inserting a script element with 
> createElement/appendChild 
> > is a great technique - in fact it's the basis for the popular JSONP 
> > method of cross-domain JSON downloads - but it doesn't 
> automatically 
> > tell you when the script is loaded. That why your developer 
> wrote that 
> > repeating interval code
> > - to check when each script is ready and load the next one 
> in sequence.
> >
> > > Awesome, one huge problem out of the way!
> >
> > Well, maybe, maybe not... :-) See below...
> >
> > > We're currently at a point where our server isn't exactly being 
> > > overloaded so i think its safe to host jquery.min on 
> google code and 
> > > the plugin (yet to be minified) and custom code possibly 
> combined in 
> > > 1 file, since there won't be a lot of custom code to begin with.
> >
> > > Also, to satisfy your curiosity, the whole purpose of using the 
> > > mts_load.js function is because this code is a part of a banner 
> > > we're planning to distribute and host in numerous 
> websites, so a lot 
> > > of them won't want scripts other than their own embedded 
> directly, 
> > > so we found that calling one simple function (mts_load) 
> that would 
> > > to all the dirty work was acceptable with them :)
> >
> > When I recommended document.write, I wasn't really thinking 
> about the 
> > whole context. There may be one good reason to use the 
> dynamic script 
> > elements instead. When you document.write the script tags, 
> the browser 
> > will wait while those script files are loaded. If you use dynamic 
> > script elements, the browser continues loading the rest of the page 
> > while the additional scripts are loaded. The only script it 
> waits for 
> > is the main loader script which is loaded with a script tag.
> >
> > If this behavior is what you want, then you can still get 
> it without 
> > all the timers and extra complication. Concatenate all of 
> your scripts 
> > (except the loader itself) into a single file. Then simply use a 
> > single dynamic script element to load that entire script. You don't 
> > need any timers or sequencing that way. Simply load that one big 
> > script, and the code in it will run in the order you expect. Very 
> > simple, and you still get the benefit of letting the page continue 
> > loading while the secondary scripts are fetched asynchronously.
> >
> > -Mike
> 

Reply via email to