- /*
- * Ajax 封裝
- *@author Kyle @time 2008-11-12
- *@author Kyle @time 2008-11-13
- *@veision 0.1
- */
- var $XhrCreator = false;// XMLHttpRequest 創造器
- /**
- * XMLHttpRequest 創造器1 用於非MS IE
- *
- * @return XMLHttpRequest
- */
- function xhr1() {
- return new XMLHttpRequest();
- }
- /**
- * XMLHttpRequest 創造器2 用於MS IE
- *
- * @return XMLHttpRequest
- */
- function xhr2() {
- return new ActiveXObject("Microsoft.XMLHTTP");
- }
- /**
- * 瀏覽器XMLHttpRequest對象創建工廠
- *
- * @return XMLHttpRequest 創造器
- */
- function XhrCreatorFactory() {
- try {
- xhr1();
- return xhr1;
- } catch (trymicrosoft) {
- try {
- xhr2();
- return xhr2;
- } catch (other) {
- alert('Browser does not support xhr');
- return null;
- }
- }
- }
- function Ajax(url) {
- /**
- * 如果XMLHttpRequest 創造器未初始化,則通過創造器工廠初始化
- */
- if (!$XhrCreator) {
- $XhrCreator = XhrCreatorFactory();// 初始化XMLHttpRequest 創造器
- }
- this.request = $XhrCreator();// 創建XMLHttpRequest對象
- this.url = url;
- this.ready = function() {
- alert('ready function is null');
- };// 當加載完成回調的函數
- }
- // 發送請求
- Ajax.prototype.send = function() {
- var request = this.request;
- var ready = this.ready;
- request.open("GET", this.url, true);
- function ready() {
- this.ready();
- }
- request.onreadystatechange = function() {
- if (request.readyState == 4)
- if (request.status == 200) {
- var data = request.responseText;
- ready(data);
- // 銷燬XMLHttpRequest對象
- }
- }
- request.send(null);// !ff中間參數不可爲空
- }
- var url='';//請求的url
- var ajax=new Ajax(url);
- //數據加載完畢是時執行
- ajax.ready=function(data){
- // 業務處理
- ajax=null;//銷燬ajax
- }
- ajax.send();//發送數據