[·Òë]High Performance JavaScript(022)
µÚÆßÕ Ajax Òì²½JavaScriptºÍXML
Ajax is a cornerstone of high-performance JavaScript. It can be used to make a page load faster by delaying the download of large resources. It can prevent page loads altogether by allowing for data to be transferred between the client and the server asynchronously. It can even be used to fetch all of a page's resources in one HTTP request. By choosing the correct transmission technique and the most efficient data format, you can significantly improve how your users interact with your site.
AjaxÊǸßÐÔÄÜJavaScriptµÄ»ùʯ¡£Ëü¿ÉÒÔͨ¹ýÑÓ³ÙÏÂÔØ´óÁ¿×ÊԴʹҳÃæ¼ÓÔظü¿ì¡£Ëüͨ¹ýÔÚ¿Í»§¶ËºÍ·þÎñÆ÷Ö®¼äÒì²½´«ËÍÊý¾Ý£¬±ÜÃâÒ³Ã漯Ìå¼ÓÔØ¡£Ëü»¹ÓÃÓÚÔÚÒ»´ÎHTTPÇëÇóÖлñÈ¡Õû¸öÒ³ÃæµÄ×ÊÔ´¡£Í¨¹ýÑ¡ÔñÕýÈ·µÄ´«Êä¼¼ÊõºÍ×îÓÐЧµÄÊý¾Ý¸ñʽ£¬Äã¿ÉÒÔÏÔÖø¸ÄÉÆÓû§ÓëÍøÕ¾Ö®¼äµÄ»¥¶¯¡£
This chapter examines the fastest techniques for sending data to and receiving it from the server, as well as the most efficient formats for encoding data.
±¾Õ¿¼²ì´Ó·þÎñÆ÷ÊÕ·¢Êý¾Ý×î¿ìµÄ¼¼Êõ£¬ÒÔ¼°×îÓÐЧµÄÊý¾Ý±àÂë¸ñʽ¡£
Data Transmission Êý¾Ý´«Êä
Ajax, at its most basic level, is a way of communicating with a server without unloading the current page; data can be requested from the server or sent to it. There are several different ways of setting up this communication channel, each with its own advantages and restrictions. This section briefly examines the different approaches and discusses the performance implications of each.
Ajax£¬ÔÚËü×î»ù±¾µÄ²ãÃ棬ÊÇÒ»ÖÖÓë·þÎñÆ÷ͨѶ¶ø²»ÖØÔص±Ç°Ò³ÃæµÄ·½·¨£¬Êý¾Ý¿É´Ó·þÎñÆ÷»ñµÃ»ò·¢Ë͸ø·þÎñÆ÷¡£ÓжàÖÖ²»Í¬µÄ·½·¨¹¹ÔìÕâÖÖͨѶͨµÀ£¬Ã¿ÖÖ·½·¨¶¼ÓÐ×Ô¼ºµÄÓÅÊƺÍÏÞÖÆ¡£±¾½Ú¼òÒªµØ½éÉÜÕâЩ²»Í¬·½·¨£¬²¢ÌÖÂÛ¸÷×Ô¶ÔÐÔÄܵÄÓ°Ïì¡£
Requesting Data ÇëÇóÊý¾Ý
There are five general techniques for requesting data from a server:
ÓÐÎåÖÖ³£Óü¼ÊõÓÃÓÚÏò·þÎñÆ÷ÇëÇóÊý¾Ý£º
• XMLHttpRequest (XHR)
• Dynamic scrip
Ïà¹ØÎĵµ£º
±¾ÎÄÖ÷ÒªÊdzöÓÚÓÐÅóÓÑʹÓÃÎÒÔÀ´Ð´µÄautocompleteµÄJS¿Ø¼þ¡£µ±Êý¾ÝÁ¿´óµÄʱºò£¬»á³öÏÖЧÂʼ«ÆäÂýµÄÇé¿ö£¬ÎÒÔÚÕâ¶Îʱ¼ä×ö³öµÄһЩ²âÊÔÒ²¼°Ò»Ð©¾Ñ飬Óë´ó¼Ò·ÖÏí£¬Èç¹ûÓдíµÄµØ·½£¬»¹ÇëÖ¸³ö¡£
¾¹ý²âÊÔ£¬ÎÒÃǻᷢÏÖÈçϵÄÇé¿ö»òÕß˵µÄ½áÂÛ£¬Èç¹ûÄúµÄ²âÊÔ½á¹ûÓëÎҵIJ»·û£¬Çë˵Ã÷ÔÒò£¬ÒÔ±ãÏ໥ѧϰ¡£
1£©µ±Ò»¸ö½Ï´óµÄHTML×Ö· ......
Ò³ÃæÌá½»Êý¾ÝÒ»°ãÓÐÁ½ÖÖ·½·¨£ºget,post¡£post¾ÍÊÇËùνµÄformÌá½»£¬Ê¹ÓÃÊÓͼ£»getÊÇͨ¹ýurlÌá½»¡£
Get·½·¨Ò»°ãÓúǫ́´úÂ루Èçasp,asp.net£©»ñµÃ²ÎÊý£¬´úÂëºÜ¼òµ¥£ºRequest.QueryString["id"];¼´¿É»ñÈ¡¡£
ÓÐЩʱºòÐèÒªÖ±½ÓÔÚǰ̨»ñÈ¡url²ÎÊý£¬ÒªÓõ½javascript£¬jsûÓÐÖ±½Ó»ñÈ¡url²ÎÊýµÄ·½·¨£¬ÄÇô£¬ÎÒÃÇÈçºÎͨ¹ýjs ......
£¼script language="javaScript"£¾
function closeWindow()
{
¡¡window.opener = null;
¡¡window.open(' ', '_self', ' ');
¡¡window.close();
}
£¼/script£¾
£¼input type='button' value='¹Ø±Õ´°¿Ú' onClick="closeWindow()"£¾
»ò
£¼input type="button" value="¹ ......
typeof ÊÇJavaScriptµÄÔËËã·û
——·µ»ØÒ»¸öÓÃÀ´±íʾ±í´ïʽµÄÊý¾ÝÀàÐ͵Ä×Ö·û´®
typeof[(] expression [)] //typeofÓï·¨ÖеÄÔ²À¨ºÅÊÇ¿ÉÑ¡Ïî
typeof ÔËËã·û@import url(../html-vss/msdnie4a.css);
typeof
ÔËËã·û°ÑÀàÐÍÐÅÏ¢µ±×÷×Ö·û´®·µ»Ø¡£typeof
·µ»ØÖµÓÐÁ ......
µÚÎåÕ Strings and Regular Expressions ×Ö·û´®ºÍÕýÔò±í´ïʽ
Practically all JavaScript programs are intimately tied to strings. For example, many applications use Ajax to fetch strings from a server, convert those strings into more easily usable JavaScript objects, and ......