开发者

Use cached version of jQuery if recent enough, otherwise download the latest

Can I do the following to shave milliseconds of loading for some users?

Fix a suitable version x of jQuery, say x = 1.4.1, and find the most recent version y of jQuery cached by the user. Then,

if (y >开发者_高级运维 x === true) {
     /* Use the cached version y of jQuery */
} else {
     /* Download and use the latest version of jQuery */
}

Also, would this be advantageous?


Caching doesn't work this way. You won't know whether or not jQuery is cached in the browser, because it won't be present in the DOM until you include a script reference to it. If that script reference happens to be to a URL that's previously been cached in that browser (your odds of this will improve if you're using a public CDN), then you get the benefits of caching for free.

In short, the only thing you can really do here to optimize "cache hits" is to use a publicly hosted copy of jQuery, like one of these.


i'd just load jQuery from google cdn because many sites use that so chances are high that it's already cached.

also i wouldn't recommend using the latest jQuery automatically because some changes could break your code


It would took too many resources and would make it even harder.

However you can check current downloaded version with

var jQueryVer = null;
if ($){
    var jQueryVer = $().jquery;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜