[·Òë]High Performance JavaScript(002)
Grouping Scripts ³É×é½Å±¾
Since each <script> tag blocks the page from rendering during initial download, it's helpful to limit the total number of <script> tags contained in the page. This applies to both inline scripts as well as those in external files. Every time a <script> tag is encountered during the parsing of an HTML page, there is going to be a delay while the code is executed; minimizing these delays improves the overall performance of the page.
ÓÉÓÚÿ¸ö<script>±êÇ©ÏÂÔØʱ×èÈûÒ³Ãæ½âÎö¹ý³Ì£¬ËùÒÔÏÞÖÆÒ³ÃæµÄ<script>×ÜÊýÒ²¿ÉÒÔ¸ÄÉÆÐÔÄÜ¡£Õâ¸ö¹æÔò¶ÔÄÚÁª½Å±¾ºÍÍⲿ½Å±¾Í¬ÑùÊÊÓá£Ã¿µ±Ò³Ãæ½âÎöÅöµ½Ò»¸ö<script>±êǩʱ£¬½ô½Ó×ÅÓÐÒ»¶Îʱ¼äÓÃÓÚ´úÂëÖ´ÐС£×îС»¯ÕâЩÑÓ³Ùʱ¼ä¿ÉÒÔ¸ÄÉÆÒ³ÃæµÄÕûÌåÐÔÄÜ¡£
The problem is slightly different when dealing with external JavaScript files. Each HTTP request brings with it additional performance overhead, so downloading one single 100 KB file will be faster than downloading four 25 KB files. To that end, it's helpful to limit the number of external script files that your page references. Typically, a large website or web application will have several required JavaScript files. You can minimize the performance impact by concatenating these files together into a single file and then calling that single file with a single <script> tag. The concatenation can happen offline using a build tool (discussed in Chapter 9) or in real-time using a tool such as the Yahoo! combo handler.
Õâ¸öÎÊÌâÓëÍⲿJavaScriptÎļþ´¦Àí¹ý³ÌÂÔÓв»Í¬¡£Ã¿¸öHTTPÇëÇ󶼻á²úÉú¶îÍâµÄÐÔÄܸºµ££¬ÏÂÔØÒ»¸ö100KBµÄÎļþ±ÈÏÂÔØËĸö25KBµÄÎļþÒª¿ì¡£×ÜÖ®£¬¼õÉÙÒýÓÃÍⲿ½Å±¾ÎļþµÄÊýÁ¿¡£µäÐ͵ģ¬Ò»¸ö´óÐÍÍøÕ¾»òÍøÒ³Ó¦ÓÃÐèÒª¶à´ÎÇëÇóJavaScriptÎļþ¡£Äã¿ÉÒÔ½«ÕâЩÎļþÕûºÏ³ÉÒ»¸öÎļþ£¬Ö»ÐèÒªÒ»¸ö<script>±êÇ©ÒýÓ㬾ͿÉÒÔ¼õÉÙÐÔÄÜËðʧ¡£ÕâһϵÁеŤ×÷¿Éͨ¹ýÒ»¸ö´ò°ü¹¤¾ßʵÏÖ£¨ÎÒÃÇÔÚµÚ9ÕÂÌÖÂÛ£©£¬»òÕßÒ»¸öʵʱ¹¤¾ß£¬ÖîÈç“Yahoo! combo handler”¡£
Yahoo! created the
Ïà¹ØÎĵµ£º
ÔÚJSÖУ¬´´½¨¶ÔÏó£¨Create Object£©²¢²»ÍêÈ«ÊÇÎÒÃÇʱ³£ËµµÄ´´½¨Àà¶ÔÏó£¬JSÖеĶÔÏóÇ¿µ÷µÄÊÇÒ»ÖÖ¸´ºÏÀàÐÍ£¬JSÖд´½¨¶ÔÏó¼°¶Ô¶ÔÏóµÄ·ÃÎÊÊǼ«ÆäÁé»îµÄ¡£
JS¶ÔÏóÊÇÒ»ÖÖ¸´ºÏÀàÐÍ£¬ËüÔÊÐíÄãͨ¹ý±äÁ¿Ãû´æ´¢ºÍ·ÃÎÊ£¬»»Ò»ÖÖ˼·£¬¶ÔÏóÊÇÒ»¸öÎÞÐòµÄÊôÐÔ¼¯ºÏ£¬¼¯ºÏÖеÄÿһÏÓÉÃû³ÆºÍÖµ×é³É£¨ÌýÆðÀ´ÊDz»ÊǺÜÏñÎÒÃdz£Ìý˵µÄHASH±í¡¢× ......
1. ¶¨ÒåÒ»¸öÓÃÓÚÊä³öJavaScript´úÂë µÄServletÀà¡£
package com.mycompany.response.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServle ......
calcu() ----º¯ÊýʵÏÖÁ½¸öÊýÏà³Ë
document.write(); ---´òÓ¡
document.calc.ÔªËØÃû.value; --»ñµÃ±íµ¥µÄÖµ
document.±íµ¥Ãû¡£±íµ¥ÔªËØÃû¡£value Ϊ±íµ¥¸³Öµ
¶¨Ò庯Êý£º
function Ãû×Ö()
{
//javaScriptÔ¤Óï¾ä
}
onClick="º¯ÊýÃû";  ......
ÀàÊÇʲô£¿
Ðí¶à¸Õ½Ó´¥±à³ÌµÄÅóÓѶ¼¿ÉÄÜÀí½â²»ÁËÀ࣬ÆäʵÀàÊǶÔÎÒÃÇÕâ¸öÏÖʵÊÀ½çµÄÄ£Ä⣬°ÑËü˵³É“Àà±ð”»òÕß“ÀàÐÍ”¿ÉÄÜ»á¸üÈÝÒ×Àí½âһЩ¡£±ÈÈç“ÈË”ÕâÖÖ¶¯
Îï¾ÍÊÇÒ»¸öÀ࣬¶ø¾ßÌåijһ¸öÈ˾ÍÊÇ“ÈË”Õâ¸öÀàµÄÒ»¸öʵÀý
£¬
“È ......
JavascriptÖеļ̳лúÖÆÊÇËùνµÄÔÐͶÔÏó¼Ì³Ð£¬Í¨¹ýÊôÐÔ·ÃÎÊ»úÖƵÄÌØÊâÐÔÀ´ÊµÏּ̳еġ£Ö®Ç°ÎÒÒ»Ö±ÒÔΪËùÓжÔÏóµÄÔÐͶÔÏó¾ÍÊÇprototypeËùÖ¸µÄÄǸö¶ÔÏ󡣺óÀ´ÔÚ¿´javascriptÉè¼ÆģʽµÄʱºò·¢ÏÖ×÷Õß×¢ÖÐÓÐÒ»¾ä»°ÓëÎÒ֮ǰµÄÀí½â²»Í¬£¬Ëû˵“ÿ¸ö¶ÔÏó¶¼ÓÐÒ»¸öÔÐͶÔÏ󣬵«Õâ²¢²»Òâζ×Åÿ¸ö¶ÔÏó¶ ......