Go to Google jsapi to host your JS Library

Source: Internet
Author: User

Nowadays, many people are getting used to writing their own JavaScript based on a variety of JS libraries, especially jquery. Nothing else. With jquery, it makes it easier and quicker for us to control page elements, so we don't need to worry too much about browser features when writing JavaScript.

However, the more powerful jquery is, the more files it has. Although today's Internet bandwidth is getting wider and wider, the speed is getting faster and faster. However, Web pages are increasingly concerned with their page size, image size, CSS size, and JS size. In fact, compared with the server running efficiency, server room bandwidth, and line quality, by optimizing the hundreds of K downloads that can be squeezed out by JS, CSS, and image files, the download speed is too limited. However, to do the development and design, we need to have such a spirit so that our work will not become larger and larger like Microsoft's operating system. From 15 floppy disks to half a CD, to one CD, to two CD, and the last DVD ......

in our JS, the biggest one is the JS library, after all, you only need to write Code to implement specific functions. However, the JS library is a bit weighty for anything that is useless. We want to use it again, but it is too big. What should we do? Split it into a lot, one more JS and one more concurrent connection ...... Fortunately, Google is quite interesting. It claims that "permanent provision" is a common JS library, and Google's server and line quality are naturally quite different. This increases the download speed and reduces the number of concurrent connections on your server. You don't need to be a fool.

it is easy to use. Just reference the relevant JS files on the Google server on the webpage. However, if you reference multiple JavaScript codes, you need to insert multiple scripts. Now there are a lot of people who can calculate one word less in code like me. Google also provides the corresponding method, that is, Google load. We only need to reference a JS file on the page to load the JS library used in real time as needed. First, add the following line of code to the page header:

<ScriptType= "Text/JavaScript"SRC= "Http://www.google.com/jsapi"> </Script>

This is enough. Google provides the following JS framework/library APIs:

Jquery

Jquery UI

Prototype

Script. aculo. Us

Mootools

Dojo

and Google APIs include all official releases of these JS frameworks/libraries. You can choose based on your needs. Using jquery as an example, we can use it as follows:

    script   type   =" text/JavaScript ">  Google. load ( "jquery" , " 1.3.2 ");  // load jquery 1.3.2  
script >

In this way, we load the JS library of jquery 1.3.2 from Google's latest CDN image, and then we can write the JS Code normally. However, even Google's CDN images take time to download. What if the browser has explained the following code before the code library is downloaded? We can set to execute JS after loading the JS Library:

 <  Script  Type  = "Text/JavaScript"> Google. Load ( "Jquery" , "1.3.2" ); // Load jquery 1.3.2 Google. setonloadcallback (Function (){ // Execute the code after loading. $ ( "Body" Pai.html ( "Hello world! " );
});
</ Script >
From: http://hi.baidu.com/kaixinguodu/blog/item/450a82ef44ff281cfcfa3cdf.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.