[·Òë]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×Ö· ......
ÍæPHP¡¢Delphi¡¢Java»ù±¾É϶¼ÓжÔÏó£¬Ï°¹ßÕâÖÖ˼·ºóÉÏÊÖÈκÎÓïÑÔ¶¼Ïë¿¿OO˼·£¬Õâ¾ø²»ÊÇÔÚ¸Ïʱ÷Ö£¬¶øÊǰÑÏà¹Ø´úÂë½øÐÐÄÚ¾ÛµÄÈ·¿ÉÒÔÌå»áµ½Î¬»¤µÄ·½±ã£¡
ÔÚJavaScriptÖÐÈçºÎ´´½¨¶ÔÏó£¿
JavaScriptÊÇ»ùÓÚ¶ÔÏóµÄ£¡ËüÒ²ÊÇÒÔObjectΪ¸ùÀ࣬ÆäËüÀà¼Ì³ÐÖ®¡£ÔÚ¸ùÀàÌṩÁ˼¸¸ö·½·¨¡£¹©¼Ì³ÐÀàʹÓã¡
ÒÔÏÂÊÇ´´½¨¶ÔÏóµÄÀý×Ó£º
funct ......
typeof ÊÇJavaScriptµÄÔËËã·û
——·µ»ØÒ»¸öÓÃÀ´±íʾ±í´ïʽµÄÊý¾ÝÀàÐ͵Ä×Ö·û´®
typeof[(] expression [)] //typeofÓï·¨ÖеÄÔ²À¨ºÅÊÇ¿ÉÑ¡Ïî
typeof ÔËËã·û@import url(../html-vss/msdnie4a.css);
typeof
ÔËËã·û°ÑÀàÐÍÐÅÏ¢µ±×÷×Ö·û´®·µ»Ø¡£typeof
·µ»ØÖµÓÐÁ ......
ʹÓÃjavascript´´½¨Microsoft XML DOM,¾Í¿ÉÒÔÍê³ÉÕâÒ»¹¤×÷.
// ×°ÈëÊý¾Ý.
var source = new ActiveXObject("Microsoft.XMLDOM");
source.async = false
source.load("history.xml");
// ×°ÈëÑùʽ±í.
var stylesheet = new ActiveXObject("Microsoft.XMLDOM");
stylesheet.async = false
stylesheet.load( ......
Splitting Up Tasks ·Ö½âÈÎÎñ
What we typically think of as one task can often be broken down into a series of subtasks. If a single function is taking too long to execute, check to see whether it can be broken down into a series of smaller functions that complete in smaller ......