值得一提的:Ajax不需要Server端的技術支援,也就是一般的Web Server就可以支援Ajax技術,Ajax完全是用Browser的技術,目的是達到非同步的請求,提高使用者友善的瀏覽體驗。這一點是我在當時學習Ajax所疑惑的問題,與大家分享。
談到Browser,也是這篇文章所撰寫的目的:談談Ajax與JavaScript。
這裡使用jQuery 1.4.1說明如何解決跨瀏覽器的相容性問題,下方這段程式碼目的是判斷瀏覽器是否支援XMLHttpRequest物件(Extensible Markup Language / Hypertext Transfer Protocol,簡稱XMLHTTP),如果不支援則使用ActiveXObject物件(這是Microsoft Internet Explorer特有的問題,版本IE5和IE6)。
ajaxSettings: {
url: location.href,
global: true,
type: "GET",
contentType: "application/x-www-form-urlencoded",
processData: true,
async: true,
/*
timeout: 0,
data: null,
username: null,
password: null,
traditional: false,
*/
// Create the request object; Microsoft failed to properly
// implement the XMLHttpRequest in IE7 (can't request local files),
// so we use the ActiveXObject when it is available
// This function can be overriden by calling jQuery.ajaxSetup
xhr: window.XMLHttpRequest && (window.location.protocol !== "file:" || !window.ActiveXObject) ?
function() {
return new window.XMLHttpRequest();
} :
function() {
try {
return new window.ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {}
},
accepts: {
xml: "application/xml, text/xml",
html: "text/html",
script: "text/javascript, application/javascript",
json: "application/json, text/javascript",
text: "text/plain",
_default: "*/*"
}
}
關於Ajax的底層技術也可以參考:
- XMLHttpRequest Object:http://msdn.microsoft.com/en-us/library/ms535874(VS.85).aspx
- Scripting and Ajax - W3C:http://www.w3.org/standards/webdesign/script
- XMLHttpRequest:http://www.w3.org/TR/2009/WD-XMLHttpRequest-20091119/
另外,使用Ajax不外乎就是操作DOM(Document Object Model,文件物件模型)和BOM(Browser Object Model,瀏覽器物件模型),兩者關係如下:
- BOM
- window物件(全域物件,BOM核心)
- location物件
- history物件
- screen物件
- navigator物件
- DOM(document物件)
- body物件
- cookie物件
其模型架構如清單所示,window物件之中有五個子物件,比方說要存取document的cookie物件,則用window.document.cookie的語法,而window可以省略,因為window是預設的全域物件,所有JavaScript宣告的物件基本上預設都在這個範疇內。
沒有留言:
張貼留言