You don't have to worry about limiting your JS file speed.

Source: Internet
Author: User

I often hear customers ask this question: Can you help me see how my website is opened so slowly? Is it poisoned?

Then we went to test his website and found that the problem was simple: Each web page started with countless external files. The style sheet is good, with a maximum of dozens of K. The terrible thing is the JS file. The owners of these companies are not IT professionals. They manage their websites part-time. They like to cater to the special effects of their leaders and do not know how to optimize them. JQUERY, special effects, verification, and so on are all listed in the head. Lin's total speed is one thousand k.

Today, let's talk about some simple Optimization Methods for them.

I. Losing weight for JS files.

Some people tend to use some JS effect code to add dazzling effects to websites. This seems to be quite popular in the last century. For now, it is best to provide users with the desired experience, use these things. There are a lot of online tools for kids shoes who want to lose weight for their JS files. They will be applied in Baidu's search and have full functions. Compressing a complete JS file is an inertial action of mainstream websites, because the compression volume is indeed considerable. The following are some good compression tools:

YUI compression tool for http://developer.yahoo.com/yui/compressor)

Dean Edwards Packer (http://dean.edwards.name/packer)

Http://crockford.com/JavaScript/jsmin (JSMin)

Ii. Minimize DOM access

It is easy to access DOM elements using JavaScript, and the code is easier to read, but the speed is slow. The following describes several key points: restrict the use of JavaScript to modify the webpage layout and cache reference for access elements. Sometimes, when your website depends on a large number of DOM changes, you should consider limiting your tag. This is a good reason to switch to HTML5 and discard the original XHTML and HTML4.

3. Use appropriate CDN

Many Web pages now use the content delivery network (CDN ). It can improve your cache mechanism because everyone can use it. It can also save some bandwidth for you. It is easy for you to use ping detection or Firebug to debug those servers, so that you can find out which aspects can speed up data. When selecting CDN, you need to take care of the location of those visitors on your website. Remember to use a public repository whenever possible.

4. Place the unworried JS file at the bottom of the page

Loading JS files at the bottom of the page is a good practice when users are more concerned about the speed experience of the website. Ease of use and users are at the top, while JavaScript is at the end. For many front-end personnel pursuing technology, this seems difficult to accept, but it should also be prepared, some users will disable JavaScript.

5. Load JS in asynchronous mode in the header

To collect various information about the website, we usually use the free statistical functions provided on the Internet, such as cnzz statistics, such as google analysis, such as Baidu statistics. The key is that, to ensure the effectiveness of statistics, we recommend that you place the statistics code in the header of the page. If you choose to do this, it may bring a very bad experience to your website users when the Statistics Code request data is unstable. However, most statistics services allow you to asynchronously load the JS files placed in the header, which largely solves this problem.

6. Package Your JavaScript into a PNG file

This method is a method recently seen on the Internet. It is very special and has not been tried yet. If you are interested, you can try it. Specifically, add your JS and CSS to the end of the image, then crop it with CSS, and obtain all the information required by the application through an HTTP request, it packs your JavaScript/css data into a PNG file, and you can also unpack it, as long as you use the getImageData () of the canvas API (). This method is highly efficient and can be compressed by about 35% without narrowing down the data.

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.