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;
}