How to improve the performance of the website to treat javascript_javascript skills
Source: Internet
Author: User
In particular, JavaScript files, when downloading it, parallel downloads are actually disabled, and will block the rendering of the page!
About JavaScript Downloads
When downloading JavaScript script files, browsers do not start other downloads in parallel, but instead allow JavaScript script files to be downloaded separately before continuing with other requests. This will be a big problem for the overall performance of the page, and the solution is as follows:
Solution 1: inline JavaScript scripts inline in the page, that is, the JavaScript script is written directly in the HTML tag.
Advantages: the fastest speed. On the home page of a large web site, it is reasonable to inline part of a JavaScript script directly into the HTML tag.
Disadvantage: JavaScript scripts are not cached separately, and other pages cannot share the JavaScript script (cannot be reused).
Solution 2: Place the JavaScript script tag link (links) at the bottom of the HTML file tag.
Requirement: The script does not contain a document.write () method to overwrite the page.
Solution 3: Use deferred (defferred) scripts. That is, adding the defer property to the Link tab indicates that the JS script does not contain the document.write () method.
Flaw: After using latency (defferred) scripts in Firefox, JavaScript scripts will still block rendering and block concurrent downloads.
But in IE, the effect is not obvious.
Summary: If a script can use the latency (defferred) scripting technique, it can definitely move to the bottom of the page!
That is, solution 3 can be completely replaced with "solution 2".
Solution 4: Use download after loading. That is, JavaScript scripts are dynamically downloaded through the OnLoad event after the page is loaded. (CSS is also common)
Advantages: It does not block the rendering of HTML pages, but also enables the reuse of JavaScript scripts (scripts will be cached in browsers).
Disadvantage: Additional JavaScript code is generated to implement this feature, adding to the complexity of the program.
Problem: May be loaded two times (inline, externally loaded once).
You can use an IFRAME to nest a page and load a JavaScript script to resolve it.
Example: http://stevesouders.com/hpws/post-onload.php
Solution 5: Dynamic inline. According to the cookie to do indicators, using code to judge, to achieve the external JS inline to the page.
Dynamic inline is the further improvement of "download after loading". Also increases the complexity of the program again.
Although JavaScript scripting is recommended at the bottom of the page, CSS stylesheets should be placed at the top of the page!
About the streamlining of JavaScript
Streamlining (minification) removes unnecessary characters, comments, and whitespace from your code to reduce the size of JavaScript code, which in turn improves the download length and loading speed of JavaScript.
Streamlining tools: Jsmin js Minifier js compression
Jsmin is used to remove all unnecessary characters, comments, and whitespace from JavaScript files.
How to use cmd: C:\Documents and Settings\xugang>jsmin <openWin.js> js_rerurn.js
1. Specify the Jsmin.exe folder first
2. openwin.js as source file
3. js_rerurn.js as Target file
Streamlining tool: Shrinksafe (formerly: Dojo compressor) Http://dojotoolkit.org/docs/shrinksafe
Shrinksafe is used to remove whitespace from JavaScript files and to shorten the name of the variable by replacing it.
How to use cmd: Java-jar shrinksafe.jar infile.js > Outfile.js
Shrinksafe.jar is the tool name
Infile.js as source file
Outfile.js as target file
Note: In the console run, to ensure that Shrinksafe.jar and Js.jar are in the same directory, and the input JS source files and output of the JS target file will also be in the same directory. (Default in C packing directory)
It is generally possible to streamline your Javascrip files using both Jsmin and Shrinksafe tools.
Compressed components
Also, don't forget to use HTTP header statements, compression scripts, style sheets, and HTML documents to reduce response time.
Request from browser client: Accept-encoding:gzip, deflate
Web server-side response: Content-encoding:gzip
Gzip is the current popular and ideal effective compression method, deflate effect is slightly inferior and less popular.
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