ajax封裝類代碼
var waitElement;
var scrollX, scrollY = -1;
function Ajax(){
////創造對象
var xmlhttp;
try
{
if (window.ActiveXObject){
/* 不要刪除以下注釋,這部分不是注釋 */
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.xmlhttp");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.xmlhttp");
} catch (e) {
xmlhttp = false;
}
}
@end @*/
}else{
xmlhttp=new XMLHttpRequest();
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}
}
catch(e)
{
alert(e.message);
}
if(!xmlhttp)
{
alert("你的瀏覽器不支援xmlhttp對象,所以一些功能無法使用,建議使用高版本的瀏覽器!!");
return;
}
///函數主題
this.getData=function(url,data,dodata)
{
var verbs =data;
// alert(data);
xmlhttp.open("POST", url,true);
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4){
waitElement.style.visibility='hidden';
dodata(xmlhttp);
}
else{
CreateWaitElement();
waitElement.style.visibility='visible';
MoveWaitElement();
}
}
xmlhttp.setRequestHeader("Content-Length",verbs.length);
xmlhttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
xmlhttp.send(verbs);
}
/////
this.getUrlData=function(url,data,dodata)
{
xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
dodata(xmlhttp);
}
else
{
waitElement.style.visibility='visible';
}
}
xmlhttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
xmlhttp.send(null);
}
this.getFormData=function(demo)
{
//alert(demo);
}
///函數主題
}
if (window.addEventListener) {
window.addEventListener('scroll', MoveWaitElement, false);
window.addEventListener('resize', MoveWaitElement, false);
}
else if (window.attachEvent) {
window.attachEvent('onscroll', MoveWaitElement);
window.attachEvent('onresize', MoveWaitElement);
}
function MoveWaitElement() {
var scrollYT, scrollXT;
if (!waitElement)
CreateWaitElement();
if (typeof(window.pageYOffset) == "number") {
scrollYT = window.pageYOffset;
scrollXT = window.pageXOffset;
}
else if (document.body && document.documentElement && document.documentElement.scrollTop) {
scrollYT = document.documentElement.scrollTop;
scrollXT = document.body.scrollLeft;
}
else if (document.body && typeof(document.body.scrollTop) == "number") {
scrollYT = document.body.scrollTop;
scrollXT = document.body.scrollLeft;
}
if (scrollX != scrollXT || scrollY != scrollYT) {
scrollX = scrollXT;
scrollY = scrollYT;
var width = document.body.clientWidth;
waitElement.style.top = scrollYT + "px";
waitElement.style.right = -scrollXT + "px";
}
}
function CreateWaitElement() {
var elem = document.getElementById('__Ajax_Loading');
if (!elem) {
elem = document.createElement("div");
elem.id = '__Ajax_Loading';
elem.style.position = 'absolute';
elem.style.height = 17;
elem.style.paddingLeft = "3px";
elem.style.paddingRight = "3px";
elem.style.fontSize = "11px";
elem.style.fontFamily = 'Arial, Verdana, Tahoma';
elem.style.border = "#000000 1px solid";
elem.style.backgroundColor = "red";
elem.style.color = "#ffffff";
elem.innerHTML = '載入中 ...';
elem.style.visibility = 'hidden';
document.body.insertBefore(elem, document.body.firstChild);
}
waitElement = elem;
}