?? ajax.js
字號:
?// JScript 文件
/**
* -------------------------------------------------------------------
*
* 版權所有: 廣州市恒浪網絡科技有限公司 使用與轉載需注明出處
* Generator: HoWave.Net 2008
* Description: DvAjax 是對一些 Ajax 常用操作的封裝(注:當前版本不支持 XmlHttp 池)
* WebSite: http://www.HoWave.net/
* Author: 林子(11403891@QQ.com)
* Creation Date: 2008-9-16
* Version: 1.0 Beta2
* Modified Date: 林子(11403891@QQ.com) @2008-9-17
*
* -------------------------------------------------------------------
*/
function DvAjax() {
// Private Fields
var _XmlHttp = null;
var _IsDisposed = false;
var _RequestHeaders = ({'Count' : 0, 'Items' : {}});
var _UserName = '', _UserPassword = '';
// Public Methods
/**
* Description 發送 HTTP 請求。
*
* 例子:
* 使用回調函數的例子:
* var ajax = new DvAjax();
* ajax.Request('post', 'http://……', 'boardi=1&topicid=100&boardtype=' + escape('版面名稱'), function (xmlHttp) {
* try
* {
* if ('0' === xmlHttp.responseText)
* {
* // Something to do
* }
* else
* {
* // Something to do
* }
* }
* finally
* {
* xmlHttp.abort();
* }
* }, true);
* 不使用回調函數的例子
* var ajax = null;
* try
* {
* var xml_http = ajax.Request('post', 'http://……', 'boardi=1&topicid=100&boardtype=' + escape('版面名稱'), null, true);
* if ('0' === xml_http.responseText)
* {
* // Something to do
* }
* else //
* {
* // Something to do
* }
* }
* finally
* {
* if (null !== ajax)
* {
* ajax.Dispose();
* ajax = null;
* }
* }
*
* @author 林子(11403891@QQ.com)
* @param string HTTP 請求的方法
* @param string HTTP 請求的 URL 地址
* @param string 要發送的數據
* @param string 回調函數(接受一個 XmlHttp 類型的參數)
* @param bool 是否禁用 XmlHttp 的緩存
* @return mixed 如果未指定回調函數,則返回一個 XmlHttp 實例的引用
*/
this.Request = function (method, url, data, callBack, disableCache) {
// 參數合法性判斷
if ('string' !== typeof(method))
{
throw new Error('參數 method 必須是一個字符串類型的值!');
}
// 參數合法性判斷
if ('string' !== typeof(url))
{
throw new Error('參數 url 必須是一個字符串類型的值!');
}
// 處理 method 參數
method = method.toLowerCase();
if ('get' !== method && 'post' !== method)
{
method = 'get';
}
// 執行一些初始化操作
_InitXmlHttp();
var async = ('function' === typeof(callBack));
try
{
// 判斷是否使用異步 XmlHttp 請求,如果是,則設置回調函數
if (async)
{
_XmlHttp.onreadystatechange = function () {
if (4 == _XmlHttp.readyState)
{
callBack(_XmlHttp);
}
};
}
// 打開 XmlHttp
if ('get' === method && 'string' === typeof(data) && data.length > 0)
{
_XmlHttp.open(method, url + ((-1 === url.indexOf('?')) ? '?' + data : '&' + data), async, _UserName, _UserPassword);
}
else
{
_XmlHttp.open(method, url, async, _UserName, _UserPassword);
}
if (_RequestHeaders.Count > 0)
{
for (var key in _RequestHeaders.Items)
{
_XmlHttp.setRequestHeader(key, _RequestHeaders.Items[key]);
}
}
// 如果參數 disableCache 被設置為 true,則不緩存 XmlHttp 請求
if (true === disableCache)
{
_XmlHttp.setRequestHeader('If-Modified-Since', '0');
}
if ('undefined' === typeof(data) || '' === data)
{
data = null;
}
if ('get' === method)
{
_XmlHttp.send(data);
}
else
{
_XmlHttp.send(data);
}
}
catch ($e)
{
_XmlHttp.abort();
throw $e;
}
// 如果未設置為異步 XmlHttp 請求,則返回 XmlHttp 實例的引用
if (!async)
{
return _XmlHttp;
}
};
/**
* Description 自動獲取 Form 表單的數據并發送到服務端
*
* 例子:
* 使用回調函數的例子:
* var ajax = new DvAjax();
* ajax.SubmitForm(document.forms['myform'], function (xmlHttp) {
* try
* {
* if ('0' === xmlHttp.responseText)
* {
* // Something to do
* }
* else
* {
* // Something to do
* }
* }
* finally
* {
* xmlHttp.abort();
* }
* }, true);
* 不使用回調函數的例子
* var ajax = null;
* try
* {
* var xml_http = ajax.Request(document.forms['myform'], null, true);
* if ('0' === xml_http.responseText)
* {
* // Something to do
* }
* else
* {
* // Something to do
* }
* }
* finally
* {
* if (null !== ajax)
* {
* ajax.Dispose();
* ajax = null;
* }
* }
*
* @author 林子(11403891@QQ.com)
* @param object 要獲取的表單對象的引用
* @param Function 要使用的回調函數(小技巧,可以傳遞匿名函數)
* @param bool 當值為 true 時,表示禁用 XmlHttp 的緩存功能
* @return mixed 當沒有指定回調函數時,將返回 XmlHttp 對象的引用,否則返回 void
*/
this.SubmitForm = function (form, callBack, disableCache) {
// 檢查參數 form 是否是有效的表單對象的引用(此處的檢查較簡單)
if (!form || 'undefined' === typeof(form.tagName) || 'form' !== form.tagName.toString().toLowerCase() || 'undefined' === typeof(form.elements))
{
throw new Error('參數 form 不是有效的表單元素的引用!');
}
var enctype = form.enctype.toString().trim().toLowerCase();
if ('multipart/form-data' === enctype)
{
throw new Error('不支持帶有文件上傳功能的表單!');
}
// 執行一些初始化操作
//_InitXmlHttp();
// 從 form 表單取得要提交的地址
var url = form.action.toString().trim();
if ('' === url) // form.action 未填寫
{
url = window.location.href;
}
else if ('?' === url.charAt(0)) // form.acton 的第一個字符是“?”
{
url = window.location.href.replace(window.location.search, '') + url;
}
// 從 form 表單取得 method 的值
var method = form.method.toString().trim().toLowerCase();
if ('get' !== method && 'post' !== method)
{
method = 'get';
}
// 處理 form 表單要提交的數據
var data = '', seperator = '', ele = null, type = '';
for (var i = 0; i < form.elements.length; i++)
{
ele = form.elements[i];
if (ele.disabled)
{
continue;
}
else if ('' === ele.name)
{
continue;
}
else if ('select' === ele.name && -1 === ele.selectedIndex)
{
continue;
}
if(ele.type)
{
type = ele.type.toString().toLowerCase();
if ('checkbox' === type && !ele.checked)
{
continue;
}
else if ('radio' === type && !ele.checked)
{
continue;
}
}
data += seperator + ele.name + '=' + escape(ele.value);
seperator = '&';
}
// 設置 Cookie
if (document.cookie)
{
this.SetRequestHeader('Cookie', document.cookie);
}
// 設置 Content-Type
this.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
// 發送 HTTP 請求
return this.Request(method, url, data, callBack, disableCache);
};
/**
* Description 設置 HTTP 請求的頭
*
* 例子:
* var ajax = new DvAjax();
* ajax.SetRequestHeader('Content-Type', 'text/html');
* // 將 cookie 信息也傳遞過去(注:ajax.SubmitForm()方法的內部實現已支持自動傳遞 cooki)
* ajax.SetRequestHeader('Cookie', document.cookie);
* ajax.Request('post', 'http://……', 'boardi=1&topicid=100&boardtype=' + escape('版面名稱'), function (xmlHttp) {
* try
* {
* if ('0' === xmlHttp.responseText)
* {
* // Something to do
* }
* else
* {
* // Something to do
* }
* }
* finally
* {
* xmlHttp.abort();
* }
* }, true);
*
* @author 林子(11403891@QQ.com)
* @param object 要獲取的表單對象的引用
* @param Function 要使用的回調函數(小技巧,可以傳遞匿名函數)
* @param bool 當值為 true 時,表示禁用 XmlHttp 的緩存功能
* @return mixed 當沒有指定回調函數時,將返回 XmlHttp 對象的引用,否則返回 void
*/
this.SetRequestHeader = function (name, value) {
// 參數判斷
if ('string' !== typeof(name))
{
throw new Error('參數 name 不是合法的字符串類型的變量!');
}
if ('string' !== typeof(value))
{
throw new Error('參數 value 不是合法的字符串類型的變量!');
}
_RequestHeaders.Count = _RequestHeaders.Count + 1;
_RequestHeaders.Items[name] = value;
};
/**
* Description 設置請求所需的用戶名和密碼
*
* 例子:
* var ajax = new DvAjax();
* ajax.SetRequestHeader('Content-Type', 'text/html');
* ajax.SetLoginInfo('UserName', 'UserPassword');
* ajax.Request('post', 'http://……', 'boardi=1&topicid=100&boardtype=' + escape('版面名稱'), function (xmlHttp) {
* try
* {
* if ('0' === xmlHttp.responseText)
* {
* // Something to do
* }
* else
* {
* // Something to do
* }
* }
* finally
* {
* xmlHttp.abort();
* }
* }, true);
*
* @author 林子(11403891@QQ.com)
* @param object 要獲取的表單對象的引用
* @param Function 要使用的回調函數(小技巧,可以傳遞匿名函數)
* @param bool 當值為 true 時,表示禁用 XmlHttp 的緩存功能
* @return mixed 當沒有指定回調函數時,將返回 XmlHttp 對象的引用,否則返回 void
*/
this.SetLoginInfo = function (userName, userPassword) {
if ('string' !== typeof(userName))
{
return false;
}
if ('string' !== typeof(userPassword))
{
return false;
}
_UserName = userName;
_UserPassword = userPassword;
};
/**
* Description 僅釋放 XmlHttp 所占用的資源。
* @author 林子(11403891@QQ.com)
* @return void
*/
this.Abort = function () {
if (null != _XmlHttp)
{
_XmlHttp.abort();
}
};
/**
* Description 釋放 XmlHttp 所占用的資源,并且使當前實例不可用。
* @author 林子(11403891@QQ.com)
* @return void
*/
this.Dispose = function () {
if (null == _XmlHttp)
{
return;
}
try
{
// 將所有狀態設為默認
_Initialize();
// 設置 _IsDisposed 為 true
_IsDisposed = true;
// 釋放 XmlHttp 對象所占用的資源
_XmlHttp.abort();
// 取消回調,便于下次使用
_XmlHttp.onreadystatechange = null;
}
catch ($e)
{
}
};
// Private Methods
/**
* Description 創建一個 XmlHttp 的實例
* @author 林子(11403891@QQ.com)
* @return object 成功創建則返回 XmlHttp 的一個實例
*/
function _CreateXmlHttp() {
if (window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
//var msxml = ['Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
var msxml = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];
for (var i = 0; i < msxml.length; i++)
{
try
{
return new ActiveXObject(msxml[i]);
}
catch($e)
{
}
}
return null;
}
/**
* Description 初始化操作
* @author 林子(11403891@QQ.com)
* @return void
*/
function _Initialize()
{
// 為 String 類型的原型增加一個 trim()
if (!('trim' in String.prototype))
{
String.prototype.trim = function () {
return this.replace(/(^\s*)|(\s*$)/g, '');
};
}
//_IsDisposed = false;
_RequestHeaders = ({'Count' : 0, 'Items' : {}});
}
// 初始化
_Initialize();
function _InitXmlHttp()
{
// 判斷是否已調用過 Dispose(),如果是,則勢出異常
if (true === _IsDisposed)
{
throw new Error('已調用過 Dispose 方法!');
}
// 初始化私有變量 _XmlHttp
if (null == _XmlHttp)
{
_XmlHttp = _CreateXmlHttp();
if (null == _XmlHttp)
{
throw new Error('您的瀏覽器不支持或未啟用 XmlHttp!');
}
}
// 如果 _XmlHttp.readyState 的值為4,表明上次對 this.Request 的調用已結束,需要關閉 HTTP 連接,然后直接返回 _XmlHttp
if (4 == _XmlHttp.readyState)
{
_Initialize();
_XmlHttp.abort();
return _XmlHttp;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -