Best way to link jQuery files

Is it better, in terms of site performance / speed, to link to jquery, for example:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 

or place files on the server and link to them from there, for example:

 <script src="js/jquery-1.9.1.js"></script> 
+6
source share
6 answers

It depends on who has the faster server, right? :)

There are several benefits to code.jquery.com :

  • This is very common. Users probably already have this file cached if they were on another site that uses this file.

  • It is probably geographically balanced. It can load faster for users who are far from your web server.

+7
source

As already mentioned, local standby is always a good idea, but you should also set IE appropriately for versions other than IE. Something as simple as this should do the trick:

 <!--[if lt IE 9]> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js""></script> <![endif]--> <!--[if gte IE 9]><!--> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <!--<![endif]--> <script> if (!window.jQuery) { document.write('<script src="/path/to/your/jquery"><\/script>'); } </script> 

The first part is conditional if for jQuery created with workarounds for older IE, with a faster and more efficient version of jQuery 2.0. This uses the Google CDN as it has versions of http and https , while code.jquery.com only has http . If https not a concern, CDN code.jquery.com usually faster.

The second part checks if window.jQuery created, and if not, use the local version.

The advantage of using the CDN version and the local version is simply speed. Not only is their server bandwidth much higher (MUCH) more than yours, most browsers have previously used this version and stored it in the cache, so the browser does not need to restart it.

+2
source

The best way to enable jQuery (or any library that uses Google Cdn):

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>')</script> 

This code associates jQuery with the CDN (this method is better because the user could ALREADY LOOK for this jQuery ver. In the browser cache). After checking this code, jQuery loaded successfully (maybe the CDN was omitted or something else ...), and if it does not bind the local version of jQuery lib to your page.

This code is used in the html5 template .

+1
source

Generally speaking, using a CDN (like code.jquery.com ) will be able to deliver files to your users faster than your own servers — what they exist for!

You may also want to use the copy on your own server as a backup if the CDN fails for some reason.

0
source

In the first case, you reference the library through a remote URL, and in the second case, it is locally available on the server.

Thus, it depends on many factors. Lastly, how far your end user is from the jquery url. The user may be closer to the jquery host or host. It will also depend on which one serves the request most efficiently.

0
source

I prefer to use googleapis:

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script> 

here why.

-1
source

Source: https://habr.com/ru/post/953500/


All Articles