5

Can I use JavaScript to check whether JQuery is (already) downloaded (cached) on the target web browser (user) or not? For Example:

If (JQuery-from-Microsoft-CDN-downloaded)
    Then use http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.js
Else if (JQuery-from-Google-APIs- downloaded)
    Then use http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
Else if (JQuery-from-code.jquery.com- downloaded)
    Then use http://code.jquery.com/jquery-1.4.4.min.js
Else use jQuery from my own website.

Means that using the ability of javascript to check whether one of them is downloaded on the target User (Web Browser), if not then use jQuery from my own website otherwise if true then use that version of JQuery that is downloaded on the target User.

Eric
  • 95,302
  • 53
  • 242
  • 374
Muaz Khan
  • 7,138
  • 9
  • 41
  • 77
  • 2
    Interesting question, but I don't think this is possible without actually loading the libraries. Although, it might be doable using an Ajax call and checking whether that returns a `304 not modified`.... Not sure whether this is practical though. – Pekka Dec 27 '10 at 08:50
  • How does a user "download" something on the target browser? – Luca Matteis Dec 27 '10 at 08:52
  • @Luca he means whether the library in question is already cached – Pekka Dec 27 '10 at 08:52
  • Yeah I meant that the library is already chached (from any server) and I want to use that cached library... – Muaz Khan Dec 27 '10 at 09:00
  • Take a look at [this question](http://stackoverflow.com/q/1447184/102441) – Eric Dec 27 '10 at 09:07

2 Answers2

4

You can't do this, another case where the same-origin policy prevents any mechanism to accomplish this. If you think about it, it makes sense...any mechanism you could use to accomplish this, you could also use to figure out which sites someone visited, by seeing which files they have cached.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • It is my idea to use the power of javascript (of the web-browser) to use cached librares (by dynamic selection) – Muaz Khan Dec 27 '10 at 10:08
  • 3
    @Hazro - I understand your idea/goal, but it's not possible...it'd be a serious privacy breach if something like this were allowed. Unfortunately there's no generic way to do this that wouldn't *also* allow it to be used for malicious purposes. – Nick Craver Dec 27 '10 at 10:13
0

There are few ways to check if jQuery is loaded

var isLoaded = (jQuery) ? true : false;

or

var isLoaded = (typeof jQuery == 'undefined') ? false : true;

If your script is loaded inside same document that jQuery included you can parse DOM and check if any of the SCRIPT tag containing link to any of the CDN's

var jQueryVersion = isLoaded ? jQuery.fn.jquery : false;
Nazariy
  • 6,028
  • 5
  • 37
  • 61
  • This will only tell you which version you've downloaded from the script tag's source url - not what has been previously cached from other page views – HorusKol Jul 29 '11 at 01:02