How can I disable a jquery include for low bandwidth connections

百般思念 提交于 2019-12-24 11:30:02

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4764780/how-can-i-disable-a-jquery-include-for-low-bandwidth-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!