AJAX (Òì²½ JavaScript ºÍ XML)
AJAX (Òì²½ JavaScript ºÍ XML) ÊǸöвúÉúµÄÊõÓï,רΪÃèÊöJavaScriptµÄÁ½ÏîÇ¿´óÐÔÄÜ.ÕâÁ½ÏîÐÔ
ÄÜÔÚ¶àÄêÀ´Ò»Ö±±»ÍøÂ翪·¢ÕßËùºöÂÔ,Ö±µ½×î½üGmail, Google suggestºÍgoogle MapsµÄºá¿Õ³öÊÀ²ÅʹÈË
ÃÇ¿ªÊ¼Òâʶµ½ÆäÖØÒªÐÔ.
ÕâÁ½Ïî±»ºöÊÓµÄÐÔÄÜÊÇ:
* ÎÞÐèÖØÐÂ×°ÔØÕû¸öÒ³Ãæ±ãÄÜÏò·þÎñÆ÷·¢ËÍÇëÇó.
* ¶ÔXMLÎĵµµÄ½âÎöºÍ´¦Àí£®
²½Öè 1 – "Çë!" --- ÈçºÎ·¢ËÍÒ»¸öHTTPÇëÇó
ΪÁËÓÃJavaScriptÏò·þÎñÆ÷·¢ËÍÒ»¸öHTTPÇëÇó, ÐèÒªÒ»¸ö¾ß±¸ÕâÖÖ¹¦ÄܵÄÀàʵÀý.
ÕâÑùµÄÀàÊ×ÏÈÓÉInternet ExplorerÒÔActiveX¶ÔÏóÒýÈë, ±»³ÆΪXMLHTTP. ºóÀ´Mozilla, Safari ºÍÆäËû
ä¯ÀÀÆ÷·×·×·ÂЧ, ÌṩÁËXMLHttpRequestÀà,ËüÖ§³Ö΢ÈíµÄActiveX¶ÔÏóËùÌṩµÄ·½·¨ºÍÊôÐÔ.
Òò´Ë, ΪÁË´´½¨Ò»¸ö¿çä¯ÀÀÆ÷µÄÕâÑùµÄÀàʵÀý(¶ÔÏó), ¿ÉÒÔÓ¦ÓÃÈçÏ´úÂë:
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
(ÉÏÀý¶Ô´úÂë×öÁËÒ»¶¨¼ò»¯,ÕâÊÇΪÁ˽âÊÍÈçºÎ´´½¨XMLHTTPÀàʵÀý.
ʵ¼ÊµÄ´úÂëʵÀý¿É²ÎÔı¾Æª²½Öè3.)
Èç¹û·þÎñÆ÷µÄÏìӦûÓÐXML mime-type header,ijЩMozillaä¯ÀÀÆ÷¿ÉÄÜÎÞ·¨Õý³£¹¤×÷. ΪÁ˽â¾öÕâ¸ö
ÎÊÌâ, Èç¹û·þÎñÆ÷ÏìÓ¦µÄheader²»ÊÇtext/xml,¿ÉÒÔµ÷ÓÃÆäËü·½·¨Ð޸ĸÃheader.
http_request = new XMLHttpRequest();
http_request.overrideMimeType('text/xml');
½ÓÏÂÀ´Òª¾ö¶¨µ±ÊÕµ½·þÎñÆ÷µÄÏìÓ¦ºó,ÐèÒª×öʲô.ÕâÐèÒª¸æËßHTTPÇëÇó¶ÔÏóÓÃÄÄÒ»¸öJavaScriptº¯Êý
´¦ÀíÕâ¸öÏìÓ¦.¿ÉÒÔ½«¶ÔÏóµÄonreadystatechangeÊôÐÔÉèÖÃΪҪʹÓõÄJavaScriptµÄº¯ÊýÃû,ÈçÏÂËùʾ:
http_request.onreadystatechange = nameOfTheFunction;
×¢Òâ:ÔÚº¯ÊýÃûºóûÓÐÀ¨ºÅ,Ò²ÎÞÐè´«µÝ²ÎÊý.ÁíÍ⻹ÓÐÒ»ÖÖ·½·¨,¿ÉÒÔÔÚìéÒ³(fly)Öж¨Ò庯Êý¼°Æä¶Ô
ÏìÓ¦Òª²ÉÈ¡µÄÐÐΪ,ÈçÏÂËùʾ:
http_request.onreadystatechange = function(){
// do the thing
};
ÔÚ¶¨ÒåÁËÈçºÎ´¦ÀíÏìÓ¦ºó,¾ÍÒª·¢ËÍÇëÇóÁË.¿ÉÒÔµ÷ÓÃHTTPÇëÇóÀàµÄopen()ºÍsend()·½·¨, ÈçÏÂËùʾ:
http_request.open('GET', 'http://www.example.org/some.file', true);
http_request.send(null);
* open()µÄµÚÒ»¸ö²ÎÊýÊÇHTTPÇëÇó·½Ê½ – GET, POST, HEAD »òÈκηþÎñÆ÷ËùÖ§³ÖµÄÄúÏëµ÷Óõķ½Ê½
. °´ÕÕHTTP¹æ·¶,¸Ã²ÎÊýÒª´óд;·ñÔò,ijЩä¯ÀÀÆ÷(ÈçFirefox)¿ÉÄÜÎÞ·¨´¦ÀíÇëÇó.ÓйØHTTPÇëÇó·½·¨µÄÏê
ϸÐÅÏ¢¿É²Î¿¼
Ïà¹ØÎĵµ£º
var
xmlDoc
=
null
;
function
parseXML
(
xmlUrl
)
{
¡¡¡¡try
{
¡¡¡¡¡¡¡¡//IE
¡¡¡¡¡¡¡¡xmlDoc
=
new
ActiveXObject
(
"Microsoft.XMLDOM"
);
¡¡¡¡¡¡¡¡xmlDoc
.
async
=
false
;
¡¡¡¡¡¡¡¡xmlDoc
......
JavaScriptʼþ´óÈ«
click() ¶ÔÏó.click() ʹ¶ÔÏó±»µã»÷¡£
closed ¶ÔÏó.closed ¶ÔÏó´°¿ÚÊÇ·ñÒѹرÕtrue/false
clearTimeout(¶ÔÏó) Çå³ýÒÑÉèÖõÄsetTimeout¶ÔÏó
clearInterval(¶ÔÏó) Çå³ýÒÑÉèÖõÄsetInterval¶ÔÏó
confirm("ÌáʾÐÅÏ¢") µ¯³öÈ·ÈÏ¿ò£¬È·¶¨·µ»ØtrueÈ¡Ïû·µ»Øfalse
cursor:Ñùʽ ¸ü¸ÄÊó±êÑùÊ ......
ClosureÖÐÎÄ·ÒëΪ±Õ°ü.×ÖÃæÉÏÀ´Àí½â¾ÍÊÇ"·â±ÕµÄ°ü".(ÕâÊÇÒ»¾ä·Ï»°)
±Õ°üÊÇʲô?
ÊéÃæ½âÊÍΪ:
Ëùν“±Õ°ü”£¬Ö¸µÄÊÇÒ»¸öÓµÓÐÐí¶à±äÁ¿ºÍ°ó¶¨ÁËÕâЩ±äÁ¿µÄ»·¾³µÄ±í´ïʽ£¨Í¨³£ÊÇÒ»¸öº¯Êý£©£¬Òò¶øÕâЩ±äÁ¿Ò²ÊǸñí´ïʽµÄÒ»²¿·Ö¡£
ÎÒÈÏΪ±Õ°ü¾ÍÊÇÄܹ»¶Á/дº¯ÊýÄÚ²¿µÄijЩ±äÁ¿µÄ×Óº¯Êý,²¢½«ÕâЩ±äÁ¿±£´æ ......
1£ºAjax¿ÉÒÔͨ¹ýJavaScriptµÄXMLHttpRequest¶ÔÏóÓë·þÎñÆ÷½øÐÐͨÐÅ£¬Í¨¹ýXMLHttpRequest£¬JavaScript¿ÉÒÔÔÚ²»ÖØÔØÒ³ÃæµÄÇé¿öÏÂÓë·þÎñÆ÷½øÐÐͨÐÅ¡£
2£ºXMLHttpRequest ä¯ÀÀÆ÷Ö§³Ö
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
xmlHtt ......
ʹÓÃ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( ......