[·Òë]High Performance JavaScript(003)
Dynamic Script Elements ¶¯Ì¬½Å±¾ÔªËØ
The Document Object Model (DOM) allows you to dynamically create almost any part of an HTML document using JavaScript. At its root, the <script> element isn't any different than any other element on a page: references can be retrieved through the DOM, and they can be moved, removed from the document, and even created. A new <script> element can be created very easily using standard DOM methods:
Îĵµ¶ÔÏóÄ£ÐÍ£¨DOM£©ÔÊÐíÄãʹÓÃJavaScript¶¯Ì¬´´½¨HTMLµÄ¼¸ºõÈ«²¿ÎĵµÄÚÈÝ¡£Æä¸ù±¾ÔÚÓÚ£¬<script>ÔªËØÓëÒ³ÃæÆäËûÔªËØÃ»ÓÐʲô²»Í¬£ºÒýÓñäÁ¿¿ÉÒÔͨ¹ýDOM½øÐмìË÷£¬¿ÉÒÔ´ÓÎĵµÖÐÒÆ¶¯¡¢É¾³ý£¬Ò²¿ÉÒÔ±»´´½¨¡£Ò»¸öеÄ<script>ÔªËØ¿ÉÒԷdz£ÈÝÒ×µØÍ¨¹ý±ê×¼DOMº¯Êý´´½¨£º
var script = document.createElement_x("script");
script.type = "text/javascript";
script.src = "file1.js";
document.getElementsByTagName("head")[0].appendChild(script);
This new <script> element loads the source file file1.js. The file begins downloading as soon as the element is added to the page. The important thing about this technique is that the file is downloaded and executed without blocking other page processes, regardless of where the download is initiated. You can even place this code in the <head> of a document without affecting the rest of the page (aside from the one HTTP connection that is used to download the file).
еÄ<script>ÔªËØ¼ÓÔØfile1.jsÔ´Îļþ¡£´ËÎļþµ±ÔªËØÌí¼Óµ½Ò³ÃæÖ®ºóÁ¢¿Ì¿ªÊ¼ÏÂÔØ¡£´Ë¼¼ÊõµÄÖØµãÔÚÓÚ£ºÎÞÂÛÔںδ¦Æô¶¯ÏÂÔØ£¬ÎļþµÄÏÂÔØºÍÔËÐж¼²»»á×èÈûÆäËûÒ³Ãæ´¦Àí¹ý³Ì¡£ÄãÉõÖÁ¿ÉÒÔ½«ÕâЩ´úÂë·ÅÔÚ<head>²¿·Ö¶ø²»»á¶ÔÆäÓಿ·ÖµÄÒ³Ãæ´úÂëÔì³ÉÓ°Ï죨³ýÁËÓÃÓÚÏÂÔØÎļþµÄHTTPÁ¬½Ó£©¡£
When a file is downloaded using a dynamic script node, the retrieved code is typically executed immediately (except in Firefox and Opera, which will wait until any previous dynamic script nodes have executed). This works well when the script is self-executing but
Ïà¹ØÎĵµ£º
±¾ÎÄÖ÷ÒªÊdzöÓÚÓÐÅóÓÑʹÓÃÎÒÔÀ´Ð´µÄautocompleteµÄJS¿Ø¼þ¡£µ±Êý¾ÝÁ¿´óµÄʱºò£¬»á³öÏÖЧÂʼ«ÆäÂýµÄÇé¿ö£¬ÎÒÔÚÕâ¶Îʱ¼ä×ö³öµÄһЩ²âÊÔÒ²¼°Ò»Ð©¾Ñ飬Óë´ó¼Ò·ÖÏí£¬Èç¹ûÓдíµÄµØ·½£¬»¹ÇëÖ¸³ö¡£
¾¹ý²âÊÔ£¬ÎÒÃǻᷢÏÖÈçϵÄÇé¿ö»òÕß˵µÄ½áÂÛ£¬Èç¹ûÄúµÄ²âÊÔ½á¹ûÓëÎҵIJ»·û£¬Çë˵Ã÷ÔÒò£¬ÒÔ±ãÏ໥ѧϰ¡£
1£©µ±Ò»¸ö½Ï´óµÄHTML×Ö· ......
¡¡´Ó2004ÄêϰëÄ꿪ʼѧϰWeb±à³ÌÖÁ½ñ3ÄêÓÐÓà¡£´ÓHTML£¬asp¿ªÊ¼µ½ÏÖÔÚµÄVS2008һ·ѧ¹ýÀ´£¬ÆäÖÐѧµÄ×î¶àµÄ»¹ÊÇ·þÎñÆ÷¶Ë±à³Ì£¬¶Ô¿Í»§¶Ë±à³ÌµÄѧϰ»¹ÊDz»³Éϵͳ¡£ËäÈ»Ôںܶà¸öϵͳÀïÃæÓ¦Óùý½Å±¾£¬ÓÐЩ»¹Æðµ½Á˱ȽÏÖØÒªµÄ×÷Óᣵ«Ò»Ö±ÊÇÖ»ÖªÆäÈ»²»ÖªÆäËùÒÔÈ»£¬ÓõÄÊÇСÐÄÒíÒí¡£ÏÖÔڽű¾±à³Ì´ÓÒÔǰµÄ"µñ³æÐ¡¼¼"±ä³ÉÁËÒ»¸öWeb¿ª ......
<SCRIPT>
//ÅжÏyÄêµÄÅ©ÀúÖÐÄǸöÔÂÊÇÈòÔÂ,²»ÊÇÈòÔ·µ»Ø0
function leapMonth(y){
return(lunarInfo[y-1900]&0xf);
}
var lunarInfo=new Array(
0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0 ......
<script language='javascript'>
function toggleAll(cb)
{
var val = cb.checked;
var frm = document.forms[0];
var len = frm.elements.length;
var i=0;
for( i=0 ; i<len ; i++)
&nb ......
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 ......