TOC

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

Introduction:

Getting started

Để dùng jQuery, bạn cần đưa nó vào trong trang mà bạn muốn dùng. Bạn có thể tải jQuery từ website jQuery.com. Luôn có lựa chọn giữa bảnProduction (bản nén) và bản Development (bản giải nén) - để học thì nên dùng bản Development.

Khi đã có file jQuery JavaScript thì bạn nên reference nó từ trang của bạn thông qua thẻ <script> HTML. Cách tốt nhất là đặt cùng trang và khai báo trong <script>

<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!