How to connect the library?
Watch the video
Next, we will explain how to connect the jQuery library to your site.
Local file on server
The easiest way to connect a library is to connect a file from the local server directory.
For this you need:
- Download the file with the library from the official site. On the site you can find the latest version of the library.
- Create a directory in the root folder of the site where our library will be stored, for example, mySite / JS / jQuery, and copy the downloaded files into it.
- Connect the file to the site. To do this, in the header <head> insert the following line:
- <script src="/js/jquery/ jquery.js"></script>
In fact, we just connected an external script. If your site uses other scripts, they should be connected after jQuery. The library is ready to use.
This method of connection is the most reliable, but it slows down the page loading somewhat.
Another option for connecting jQuery to a site is using a CDN.
CDN - Content Delivery Network - a way to download files from a remote server, which allows you to increase the speed of loading the page due to the fact that the desired file can already be saved (cached) on the user's computer.
To do this, you must specify a link to the external API, for example:
- <script src = "// code.jquery.com/jquery-1.11.0.min.js"> </ script>.
In this case, the link to code.jquery.com was used. There are many different servers, the most popular of them: jquery, Google and Cloudflare.
This method will increase the page loading speed, but at the same time make your site dependent on the operation of other servers.
Combination of local file and CDN
The best option would be a combination of the two presented methods. First, the library file is loaded from the CDN, and if it is not loaded, the local file is loaded from the server.