TOC
Need to learn JavaScript? jQuery is a JavaScript framework, so if you don't already know about the JavaScript programming language, we recommend that you learn it now: Learn JavaScript

This article is currently in the process of being translated into Finnish (~5% done).

Introduction:

Getting started

To use jQuery, you need to include it on the pages where you wish to take advantage of it. You can do this by downloading jQuery from their website at jQuery.com. There is usually a choice between a Production (compressed) version and a Development (uncompressed) version - for learning, we recommend the Development version.

Yksinkertaisinta on tallentaa jQuery.js javascript-tiedosto samaan hakemistoon HTML-dokumentin kanssa, jolloin saat sen käyttöösi seuraavan esimerkin mukaisesti viittaamalla siihen <script> HTML tagilla dokumentin <head> osiossa:

<script type="text/javascript" src="jquery-3.3.1.js"></script>

A part of your page should now look something like this:

<head>    
<title>jQuery test</title>    
<script type="text/javascript" src="jquery-3.3.1.js"></script>    
</head>

A more modern approach, instead of downloading and hosting jQuery yourself, is to include it from a CDN (Content Delivery Network). There are many CDN's out there, including ones from Google and Microsoft, who are hosting several different versions of jQuery and other JavaScript frameworks. It saves you from having to download and store the jQuery framework, but it has a much bigger advantage: Because the file comes from a common URL that other websites may use as well, chances are that when people reaches your website and their browser requests the jQuery framework, it may already be in the cache, because another website is using the exact same version and file. Besides that, most CDN's will make sure that once a user requests a file from it, it's served from the server closest to them, so your European users won't have to get the file all the way from the US and so on.

You can use jQuery from a CDN just like you would do with the downloaded version, only the URL changes. For instance, to include jQuery 3.3.1 from Google, you would write the following:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I suggest that you use this approach, unless you have a specific reason for hosting jQuery yourself. Here is a link to the jQuery CDN information from Google:

Google jQuery CDN

Here's the information about using jQuery from a CDN from the official jQuery website:

code.jquery.com

Summary

Using jQuery can be accomplished by downloading the library or adding a reference to it on one of the many CDNs. Either way, you will usually get the choice between an uncompressed developer version and a compressed production version. For development and testing, use the uncompressed version to get more precise errors and understanding of the library. For production, you should use the compressed version to save bandwidth for your end-users, which will also result in faster load times for your website.

As soon as you have added jQuery to your page, as described above, you are ready to move on and learn more about jQuery in the coming chapters!


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!