views:

137

answers:

6

Having read up recently on yahoo's web optimisation tips and using YSlow I've implemented a few of their ideas on one of my sites http://www.gwynfryncottages.com you can see the file here http://www.gwynfryncottages.com/js/gw-custom.js.

While this technique seems to work perfectly on most occasions, and really does speed up the site, but I do notice a significantly higher number of errors where the javascripts don't load or don't load completely while I'm working on the site so three questions:-

  • is combining scripts this way a good idea at all in terms of reliability?

  • is there any way to measure the number of errors i.e. the number of times the script failed to load?

  • is there any way to 'pre-load' the javascript or ensure that the number of loading errors is reduced?

A: 

Firebug is a nice tool that helps developing javascript application and let you see the errors and debug it. Furthermore it shows you the network activity of your site and what is going wrong.

There is also the error console in firefox that shows you the errors.

hara
I do use firebug and the FF console, while the site does show some errors these mostly seem to happen when the s3slider reloads and they identify lines in jQuery its self so I took them to be a problems within the plug in rather than problems I should fix - good or bad idea?
toomanyairmiles
A: 

I can't see the load function in your code which is being called on your body tag! I'd try and steer clear of adding JS to your HTML file, it can be added dynamically and will prob cause you less hassle along the way aas well as being easier to maintain.

I'd say that the things you need to look out for are making sure that you're not trying to call something before it's defined (maybe your seperate JS files were defined in a different order to how they appear in the single JS file).

Firebug for firefox is a good development tool, if you've not found it already. Webkit, Opera and IE also have various other dev tools.

Martyn
The onload in the body tag is a hook for google maps which is used on some pages in the site and most of the blog entries, for utility purposes it ended up in the head include for the whole site as I couldn't get it to work any other way.I've played with the order of the files fairly extensively and the way it is at the moment seems to work best - is there something specific you noticed?
toomanyairmiles
+5  A: 

Of course it's good. You will not only decrease HTTP requests but you will cut down delays in downloading other resources.

Try using minify: http://code.google.com/p/minify/, I've been using it and I've no complaints.

I can assure you that combining files WON'T cause any errors as a combined script is the same as 10 non-combined scripts, they all load in the same way (In an ordered way, left to right, top to bottom). Double check the way you're combining them.

Ben
My concern with minifying the big file was that components of it are already minified - would doing it a second time not cause more problems than it would fix? Google page speed does recommend it, but then i notice it also recommends minifying it's own ga.js file!
toomanyairmiles
I *LOVE* Minify. I use it on my web application http://my.perqworks.com - As a self-obsessed performance tweaker, combining, minifying, and cache my http resources just feels awesome.
Christopher Altman
I know what you mean about tweaking performance, it does leave an afterglow, but how much browser testing do you have to do?
toomanyairmiles
@Christopher Altman: Then I suggest you to update the code in use to be jQuery 1.4.2, not 1.4.0. The first one had some bugs... AND DON'T USE TABLES FOR LAYOUT!! :D .. Just a heads-up.
Sune Rasmussen
@toomanyairmiles - If it is minified it won't minify again, that's not a problem. Remember that minify and pack are two different things. Minifying minified JS is not a problem, packing a packed JS is.
Ben
@Ben, thanks for clearing that up, I'll give it a go.
toomanyairmiles
A: 

Script execution stops at serious errors. If you have multiple scripts, the others will still run; if you packed everything into one big file, a lot more code won't get executed. So combining scripts is bad for reliability, but can be good for other purposes (mainly load time).

All browsers have some sort of javascript console which will show you the number of errors. Most have some sort of developer tool too (Firebug in Firefox, Dragonfly in Opera etc).

I'm not sure what you mean by preloading. Since a javascript file can affect the rest of the page in various ways, browsers will fully load and execute a script tag before continuing to parse the page (which is why scripts can slow page loading down so much).

Tgr
I'm a designer type rather than a coder, so I was thinking about image pre-loading and wondering if something similar existed in the javascript world. Beyond that, I'm thinking about putting scripts at the bottom of the page etc and wondering if that works at all.
toomanyairmiles
A: 

Combining JavaScript files is always the best way to go, unless it's not logically sane to do so (downloading jQuery from Google Code instead of hosting it yourself is a good example).
I always combine as many files as I can (JavaScript, CSS, images (CSS Sprites), etc.), also in development, and I never experience any problems. It's way faster regarding less http connections, which should not in any case be underestimated.

Regarding that you want to count the errors, I don't exactly see what you mean. But, debugging tools like the built in one in Google Chrome or Firebug for Firefox are good tools for debugging your JavaScript code, and shows lists of the errors occurring.

And to that thing of preloading: Yes, it can be done, though it'll become nasty and illogical. However, I can't think of any case whatsoever where it would be a good solution to have the trouble to preload the JavaScript, compared to just make it work right out of the box, no error checking needed.

About the error you are experiencing, the only one that my Chrome points out is this:

Uncaught ReferenceError: load is not defined

... which seems to be the onload method "load()" set on line 55 of your HTML document when the body tag is started.

Sune Rasmussen
In terms of counting errors, I meant load errors, the number of times the file fails to load completely, the onload is a hook for google maps, for maintenance reasons it ended up in the header include file as it's needed on so many pages that the error was less of a problem than it's absence.
toomanyairmiles
A: 

That is a helluva script file.

You have a few evals in it, they may give problems with some minifiers, but you can always use one that simply removes whitespace and comments, that should still cut a fair amount from your file.

It is generally safe to minify minified code, it shouldn't be a worry that some of it is already minified.

eBusiness