如何检查jQuery是否已在缓存中? [英] How to check if jQuery is already in cache?
问题描述
无论如何,有没有检查客户端是否之前从CDN加载过jQuery?我的意思是拥有这样的代码:
Is there anyway to check on a client if jQuery was loaded before from a CDN? I mean to have code like this:
if (jQuery.isLoadedFromCDN)
//DoNothing
else
//load from internal resource
请注意,我不想检查是否已加载jQuery,特别是如果它是从CDN加载的.
NOTE that I don't want to check that jQuery is loaded, but specifically if it was loaded from a CDN.
上下文是,我有一个使用jQuery的内部LAN Web应用程序,从LAN加载jQuery绝对更快,但是如果以前已经从CDN下载过(可能是这种情况),那么我只想使用它否则,我想从我们的内部资源而不是CDN中获取它.带宽节省绝对不是很大,但是我很想知道这在技术上是否可行.
The context is that I have an internal LAN web app that uses jQuery, loading jQuery from the LAN is definitely faster but if it already was downloaded before from a CDN (which is probably the case) then I just want to use that otherwise I want to get it from our internal resource not the cdn. The bandwidth saving is definitely not huge, but I am more curious to know if that's technically possible.
推荐答案
I'm guessing you're checking to see if jQuery was loaded from the Google Libraries API?
您的浏览器将缓存jQuery,无论它是来自CDN还是来自您的LAN.如果从CDN进行的先前检索中已将其缓存在缓存中,则它将比从LAN加载更快.如果上次访问您的站点或使用相同CDN的另一个站点尚未将其存储在缓存中,则只需加载一次:后续访问将从缓存中加载.
Your browser will cache jQuery whether it's from the CDN or from your LAN. If it's already in the cache from a previous retrieval from the CDN, it'll load faster than from your LAN. If it's not already in your cache from either a previous visit to your site, or another site using the same CDN, it'll only need to load once: subsequent visits will load from the cache.
在CDN和您的LAN之间分割jQuery的URL只会导致两个副本被缓存.让浏览器缓存执行其应有的功能. :)
Splitting the URL for jQuery between the CDN and your LAN will just cause two copies to get cached. Let the browser cache do what it was meant to do. :)
这篇关于如何检查jQuery是否已在缓存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!