?? oajax.lib.js
字號:
?/*
By Hangring
#2007.01.11#
---
use list:
> global.lib.js
> browser.lib.js
---
oAjax
*/
function oAjax () {
this.req = null;
this.url = '';
this.content = '';
this.type = 'text';
this.encode = '';
this.asyn = true;
this.action = 'get';
this.error = false;
}
oAjax.prototype.init = function () {
if (window.XMLHttpRequest) {
this.req = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
// isIE = true;
try {
this.req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
this.req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
this.req = false;
}
}
}
var self = this;
if (this.req) {
this.req.onreadystatechange = function () {self.listener()};
}
};
oAjax.prototype.listener = function () {
if (this.req.readyState == 4) {
if (this.req.status == 200) {
// right
try {
this.callback(Browser.IsIE && this.encode == 'gb2312' ? oAjax.gb2utf8(this.req.responseBody) : (this.type == 'text' ? this.req.responseText : this.req.responseXML));
}
catch (e) {
this.halt('[callback] ' + e.name + ':' + e.message);
}
}
else {
// error
this.halt('[callback error] ' + this.req.status);
}
}
};
oAjax.prototype.send = function (url) {
this.init();
url = this.url = url || this.url || '';
this.content = !!this.content ? this.content : '';
this.encode = this.encode ? this.encode.toLowerCase() : '';
this.asyn = this.asyn == undefined ? true : !!this.asyn;
this.action = (this.action == undefined || this.action == 'get') ? 'Get' : 'Post';
this.error = this.error == undefined ? false : !!this.error;
if (! url && this.error) {
alert('Ajax請求URL不能為空。');
return;
}
try {
this.req.open(this.action, url, this.asyn);
}
catch (e) {
this.halt('[open] ' + e.name + ':' + e.message);
return;
}
try {
this.req.setRequestHeader('Connection', 'close');
this.req.setRequestHeader('Accept-Encoding', 'gzip, deflate');
this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded' + (this.encode ? ';charset=' + this.encode : ''));
if(this.req.overrideMimeType && this.encode) {
this.req.overrideMimeType('text/xml' + (this.encode ? ';charset=' + this.encode : ''));
}
this.req.send(this.content);
}
catch (e) {
this.halt('[open] ' + e.name + ':' + e.message + '\n** 檢查是否為跨域訪問。');
}
};
oAjax.prototype.callback = function (content) {
//alert(content);
};
// abort
oAjax.prototype.abort = function () {
this.req.abort();
};
oAjax.prototype.halt = function (description) {
this.error && alert(description);
};
// gb2312 to utf8
oAjax.gb2utf8 = function (data) {
var glbEncode = [];
gb2utf8_data = data;
execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript");
var t = escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2");
t = t.split("@");
var i=0, j = t.length, k;
while(++i < j) {
k = t[i].substring(0,4);
if(!glbEncode[k]) {
gb2utf8_char = eval("0x" + k);
execScript("gb2utf8_char = Chr(gb2utf8_char)", "VBScript");
glbEncode[k] = escape(gb2utf8_char).substring(1, 6);
}
t[i] = glbEncode[k] + t[i].substring(4);
}
gb2utf8_data = gb2utf8_char = null;
return unescape(t.join("%"));
}
/*
var ajax = new oAjax();
// 發送的內容:a=b&b=c&c=d
ajax.content = '';
ajax.action = 'get'|'post';
// 是否顯示錯誤
ajax.error = true|false;
// 異步或同步
ajax.asyn = true|false;
// 請求的頁面的編碼為'gb2312'或空
ajax.encode = ''|'gb2312';
// 回調函數
ajax.callback = function (content) {
// 處理返回內容
};
ajax.send(url);
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -