1

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).

Thomas Rynne
  • 397
  • 2
  • 10
  • Bandwidth testing seems like a really bad idea, since in **all** cases it's going to be perceived by the user as simple latency (and it's worse on slower connections, exactly when you don't want it to be worse :-) – Pointy Jan 21 '11 at 23:13
  • Why not let the user pick? Load the default page output with jQuery and then give users the option to switch to a low-bandwidth alternative. – Nathan Taylor Jan 21 '11 at 23:13
  • You might avoid the jQuery download anyway by using one of the CDN versions (like Google's), so that the user might already have jQuery in cache. – Andrew Jan 21 '11 at 23:15

2 Answers2

6

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.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 1
    I agree. In addition, if you include all your scripts at the bottom of the page, the user will be able to see all the content before jQuery even starts to download. – David Tang Jan 21 '11 at 23:22
0

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 :)

japrescott
  • 4,736
  • 3
  • 25
  • 37