Reputation: 25165
I'm loading jQuery via Google's CDN using the following code.
My main question is what will happen if a user hits my site and hasn't yet got jQuery pre-cached. Will he download the Google version and my own? How does concurrency here work?
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
if(typeof jQuery == 'undefined') {
//<![CDATA[
document.write("<script src='/includes/jquery-1.4.2.min.js' type='text/javascript'><\/script>");
//]]>
}
</script>
Thanks.
Upvotes: 8
Views: 1499
Reputation: 630627
In your example code they will download the google version if they don't already have it because of another site. Then if for some reason google is down, they'll download your version, they won't download both. The second is only requested if the first (from Google) fails.
The check goes like this:
jQuery
(the JavaScript object) defined?
if()
is false, continue on.<script>
tag just added.Upvotes: 9