I have a simple website which makes a little use of jquery but it is not essential so I would like to skip the jquery download if the users connection is slow.
Ideally I would detect a slow connection but I suppose detecting a mobile phone might be a reasonable proxy for this.
If this is possible I would then remove the parts of the page which use jquery and add them dynamically if jquery is loaded.
Options I have considered are:
- Test the connection speed by downloading a small image
- Download jquery but timeout after a second or so (I searched for cancelling downloads but found nothing)
- Drop jquery all together and just write the javascript I need (I'll probably get it wrong for some browsers though)
- Wrap the jquery download in some javascript which checks the useragent for andriod or iphone
- Try to use css media selectors to control the jquery download
but I am not sure which to persue.
Also, I can't use server side tricks as it is a static page (to avoid the appengine cold start issue).
I tend to say forget it.
Any serious speed test will need to transfer more data than the jQuery library weighs.
You could give users a choice (like "open low-bandwidth web site") and store that choice in a cookie. You could then check the cookie's value, and embed or not embed jQuery dynamically on server side, or using JavaScript as shown in @banjomonster's answer to this question.
Other than that, I would just go ahead and include it. If you include it from Google's CDN, chances are it is in the user's browser already anyway, and doesn't have to be loaded again.
I agree with my precursors. jQuery will most likely be in the users cache, in the ISPs cache or served very fast from a CDN, such as Google's.
A >real< speed test can't be done in your szenario. (also, "normal" Speed test, downing/upping 1MB are very random in their result)
Just looking at the speed analysis, you could start a timer in the head section of the page and measure the time it took to get in the onLoad event/ bottom of the page (where you would load jquery and your other javascript files and execute them).
The timegap would tell you something about the speed. But that number would be deluded by the users system and other apps running (slowing down execution speed) as well as your server maybe beeing overloaded and not responding as snappy.
As I read, you are thinking about "Dropping jquery all together and just write the javascript you need".
I'd like to point out that Google Closure Compiler looks at all your code and functions your using and strips out all functions you never need, minifying your code significantly!!
Also, I would consider using Zepto instead of jQuery - it has the same function set as jquery with a much lighter footprint (but only supporting mordern browser). And its written by Thomas Fuchs (Script.acolo.us fame), which is a brainiac like John Resig (jQuery) is :)