jQuery – CDN
A CDN is a network of servers distributed geographically to deliver web content more efficiently to users. Using a CDN to host libraries like jQuery can improve website performance by reducing latency and increasing accessibility.
Benefits of Using jQuery CDN
- Faster Load Times: CDN servers are strategically located to deliver content quickly, resulting in faster load times for jQuery and other resources.
- High Availability: CDN servers are often replicated across multiple locations, ensuring high availability and reliability.
- Caching: CDN providers often cache files, reducing server load and bandwidth usage for both the developer and end-users.
Using jQuery CDN
To include jQuery in your web project using a CDN, simply add a <script>
tag with the CDN URL in the <head>
section of your HTML file.
Example:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>jQuery CDN Example</title> <!-- Include jQuery via CDN --> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <h1>jQuery CDN Example</h1> <p>This paragraph will be modified using jQuery.</p> <button id="btnClick">Click Me</button> <script> // jQuery code goes here $(document).ready(function() { $("#btnClick").click(function() { $("p").text("jQuery is awesome!"); }); }); </script> </body> </html> |
In this example, we include jQuery from the CDN by adding <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
in the <head>
section. Then, we use jQuery to modify the text of a paragraph when a button is clicked.
Conclusion
Using jQuery via CDN is a convenient and efficient way to include the library in your web projects. By leveraging CDN benefits such as faster load times and high availability, developers can enhance the performance and accessibility of their websites.