DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> AJAX Hacks 之HACK1. 檢測浏覽器的兼容性
AJAX Hacks 之HACK1. 檢測浏覽器的兼容性
編輯:AJAX詳解     
AJax Hacks 之HACK1. 檢測浏覽器的兼容性

本節介紹如何使用JavaScript 建立起IE或Mozilla浏覽器相應的請求對象。客戶端使用的浏覽器是各種各樣的。因此也有不同的請求對象。如在Firefox, Netscape, Safari,Opera中是XMLHttpRequest。IE則是Microsoft.XMLHTTP 或 Msxml2.XMLHTTP。



使用AJax的第一步是檢測客戶浏覽器的類型,根據相應的類型取得request 對象。下面就是取得該對象的完整代碼:



/* Initialize a Request object that is already constructed /
function initReq(reqType,url,bool){
/
Specify the function that will handle the HTTP response */
request.onreadystatechange=handleResponse;



/* onreadystatechange監聽handleResponse函數。接下來就是打開連接,發送請求……



*/
request.open(reqType,url,bool);
request.send(null);
}



/* Wrapper function for constructing a Request object.
Parameters:
reqType: The HTTP request type such as GET or POST.
url: The URL of the server program.
asynch: Whether to send the request asynchronously or not. */



function httpRequest(reqType,url,asynch){
//Mozilla-based browsers
if(window.XMLHttpRequest){    //如果是非IE浏覽器
request = new XMLHttpRequest(); //取得對象。
initReq(reqType,url,asynch);
} else if (window.ActiveXObject){ //如果是IE浏覽器
request=new ActiveXObject("Msxml2.XMLHTTP");
if (! request){
request=new ActiveXObject("Microsoft.XMLHTTP");
}
if(request){
initReq(reqType,url,asynch);
/* Unlikely to branch here, as IE users will be able to use either one of the
constructors*/
} else {
alert("Your browser does not permit the use "+
"of all of this application's features!");}
} else {
alert("Your browser does not permit the use "+
"of all of this application's features!");}
}



應當注意到handleResponse函數是事件處理的核心。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved