[·Òë]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
Ïà¹ØÎĵµ£º
£¼script language="javaScript"£¾
function closeWindow()
{
¡¡window.opener = null;
¡¡window.open(' ', '_self', ' ');
¡¡window.close();
}
£¼/script£¾
£¼input type='button' value='¹Ø±Õ´°¿Ú' onClick="closeWindow()"£¾
»ò
£¼input type="button" value="¹ ......
Òþ²Ø³ÉÔ±±äÁ¿
ÔÚº¯ÊýÌåÄÚ¶¨ÒåµÄ±äÁ¿Îª¾Ö²¿±äÁ¿£¬À뿪º¯Êý¾Í¹ÒµôÁË
ÔÚº¯ÊýÌåÄÚʹÓÃthis.³ÉÔ±±äÁ¿Ãû£¬ÔòΪwindow¶ÔÏó¼¶±äÁ¿£¬¼´È«¾Ö±äÁ¿
¹ÊÐèÒªÕâÑùÒþ²Ø³ÉÔ±±äÁ¿£¬ÏòÍâÖ»±©Â¶get¡¢setº¯Êý
function testClass(name){
var _firstname=name;
return {
getname : function() {
return _fir ......
µÚËÄÕ Algorithms and Flow Control Ëã·¨ºÍÁ÷³Ì¿ØÖÆ
The overall structure of your code is one of the main determinants as to how fast it will execute. Having a very small amount of code doesn't necessarily mean that it will run quickly, and having a large amount of code ......
Recursion Patterns µÝ¹éģʽ
When you run into a call stack size limit, your first step should be to identify any instances of recursion in the code. To that end, there are two recursive patterns to be aware of. The first is the straightforward recursive pattern represented ......
µÚÎåÕ 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 ......