wzhu
(wzhu)
版主
  
C[A-Z]O
UID 68040
精华
0
积分 47793
帖子 1569
金钱 47770 喜悦币
威望 0
人脉 23
阅读权限 100
注册 2006-1-14 来自 湖北荆州>陕西西安>辽宁 ...
状态 离线
|
[广告]: 代充Paypal帐号美元
欢迎优化、指教( Ajax )
以下是我自己平时用Ajax的核心部分。
不需要什么特效,只要保证兼容。不出错。
欢迎优化,指教, 拍砖。
<?php function XHR() { this.m_Version = "2006-12-04"; this.m_XmlHttp = false; /* xmlHttpRequest Object */ this.m_IsXml = false; /* responseXML Or responseText */ this.m_Method = "GET"; this.m_IsAsync = true; this.m_IsCache = false; this.m_Charset = "UTF-8"; this.m_Aborted = false; this.m_TimeOutID= null; this.m_Delay = 10000; /* 10s */
this.m_AlertLevel = "E"; /* Alert Switch */ this.m_QueryString = ""; this.m_RequestUrl = "./"; this.m_MimeValue = "text/html"; this.m_CopyRight = "nnCopyright 漏 2006-2008n wzhu China. All Rights Reserved.";
this.setSync = function(){ this.m_IsAsync = false; }; this.setAsync = function() { this.m_IsAsync = true; }; this.setCacheOn = function() { this.m_IsCache = true; }; this.setCacheOff = function() { this.m_IsCache = false; }; this.setGetMethod = function(){ this.m_Method = "GET"; }; this.setDebugOn = function (){ this.m_AlertLevel = "D"; }; this.setWarnOn = function (){ this.m_AlertLevel = "W"; }; this.setErrorOn = function (){ this.m_AlertLevel = "E"; };
this.setCharset = function ( charset ){ this.m_Charset = charset; }; this.setRequestUrl = function ( url ){ this.m_RequestUrl = url; }; this.setQueryString = function ( str ){ this.m_QueryString = str; }; this.setAbort = function (){ this.m_Aborted = true; }; this.setXmlMime = function (){ this.m_IsXml = true; this.m_MimeValue = "text/xml"; };
this.setHtmlMime = function (){ this.m_IsXML = false; this.m_MimeValue = "text/html"; };
this.setPostMethod = function(){ this.m_Method = "POST"; this.m_MimeValue = "application/x-www-form-urlencoded"; };
this.debugMSG = function( content ){ if( this.m_AlertLevel == "D" ){ alert( content + this.m_CopyRight ); } };
this.warnMSG = function( content ){ if( this.m_AlertLevel != "E" ){ alert( content + this.m_CopyRight ); } };
this.errorMSG = function( content ){ alert( content + this.m_CopyRight ); };
this.init = function (){ this.setAsync(); this.setGetMethod(); this.setDebugModeOff(); this.setCacheOff(); this.setXmlMime(); this.setCharset( "UTF-8" ); this.queryString = ""; };
this.createXmlHttpRequest = function(){ /* Microsoft Internet Explorer */ /*@cc_on @*/ /*@if (@_jscript_version >= 5) if ( window.ActiveXObject && !window.XMLHttpRequest ) { var MSXMLS = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]; for ( var i = 0; i < MSXMLS.length; i++ ) { try { this.debugMSG( MSXMLS[i] ); this.m_XmlHttp = new ActiveXObject( MSXMLS[i] ); return; }catch (E) { this.warnMSG( E.number+":"+E.description ); continue; } } this.m_XmlHttp = false; this.errorMSG( "Your Brower Can't Support Ajax!" ); return; } @end @*/ /* Mozilla FireFox */ if( window.XMLHttpRequest ){ try{ this.m_XmlHttp = new XMLHttpRequest(); return; }catch( E ){ this.m_XmlHttp = false; this.warnMSG( E.number+":"+E.description ); } } this.errorMSG( "Your Brower Can't Support Ajax!" ); }; /* Just For No Cache */ this.addTimeStamp = function(){ this.m_QueryString += "timestamp=" + new Date().getTime(); };
this.addParameter = function( name, value ){ this.m_QueryString += name + "=" + encodeURIComponent( value ) + "&"; };
this.doRequestUsingPost = function() { if( !this.m_IsCache ){ this.addTimeStamp(); } this.m_XmlHttp.open( this.m_Method, this.m_RequestUrl, this.m_IsAsync ); //this.m_XmlHttp.open( this.m_Method, this.m_RequestUrl ); this.setCommonHeader(); this.debugMSG( this.m_QueryString ); /* Post /a.php HTTP/1.1 */ this.m_XmlHttp.setRequestHeader( "Method", "POST " + this.m_RequestUrl + " HTTP/1.1" ); this.m_XmlHttp.setRequestHeader( "Content-Length",this.m_QueryString.length ); this.m_XmlHttp.send( this.m_QueryString ); };
this.doRequestUsingGet = function() { var queryString; if( !this.m_IsCache ){ this.addTimeStamp(); } if( this.m_RequestUrl.indexOf("?") > 0 ){ queryString = this.m_RequestUrl + "&" + this.m_QueryString; } else { queryString = this.m_RequestUrl + "?" + this.m_QueryString; }
this.debugMSG( queryString ); this.m_XmlHttp.open( this.m_Method, queryString, this.m_IsAsync ); this.setCommonHeader(); this.m_XmlHttp.send( null ); };
this.setCommonHeader = function(){ if( this.m_XmlHttp.overrideMimeType && this.m_IsXml ) { this.m_XmlHttp.overrideMimeType( "text/xml" ); }
this.m_XmlHttp.setRequestHeader( "Content-Type", this.m_MimeValue + "; charset=" + this.m_Charset ); if( !this.m_IsCache ){ this.m_XmlHttp.setRequestHeader( "Cache-Control", "no-cache" ); this.m_XmlHttp.setRequestHeader( "If-Modified-Since", "0" ); } };
this.Execute = function(){ this.createXmlHttpRequest(); if( this.m_XmlHttp ){ this.doFuncStart(); var self = this; self.m_TimeOutID = window.setTimeout( function() { self.m_Aborted = true ; }, self.m_Delay ); this.m_XmlHttp.onreadystatechange = function(){ self.debugMSG( self.m_XmlHttp.readyState ); switch ( self.m_XmlHttp.readyState ) { case 4 : window.clearTimeout( self.m_TimeOutID ); if( self.m_Aborted ){ self.m_XmlHttp.abort(); self.warnMSG( "TimeOut" ); self.doFuncFinish(); return false; } if( self.m_XmlHttp.status == 200 ) { self.debugMSG( self.m_XmlHttp.getAllResponseHeaders() + "n" + self.m_XmlHttp.responseText ); if( self.m_IsXml ){ try{ /* xmlDoc IS Object */ var xmlDom = self.m_XmlHttp.responseXML; //** if( xmlDom.documentElement == null && self.m_XmlHttp.responseText != null ) { if ( window.ActiveXObject ){ var MSXMLDOMS = ["Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "Msxml2.DOMDocument", "Microsoft.XmlDom"]; for ( var i = 0; i < MSXMLDOMS.length; i++ ) { try { self.debugMSG (MSXMLDOMS[i] ); xmlDom = new ActiveXObject( MSXMLDOMS[i] ); break; }catch( E ){ self.warnMSG( E.number+":"+E.description ); } } xmlDom.validateOnParse = true; if( !xmlDom.loadXML( self.m_XmlHttp.responseText ) ){ throw "XML DOM parsing failed:n" + xmlDom.parseError.reason; } } else if( document.implementation && document.implementation.createDocument ) { self.debugMSG( "implementation" ); xmlDom = document.implementation.createDocument( "","",null ); var oParser = new DOMParser(); var xmlDom = oParser.parseFromString( self.m_XmlHttp.responseText,"text/xml"); if ( xmlDom.documentElement.tagName == "parsererror" ) { throw "XML DOM parsing failed";// + xmlDom.documentElement; } } } //*/ }catch( E ){ self.errorMSG( E ); self.doFuncFinish(); return false;
} /* Do Something After Response */ self.doFuncXml( xmlDom ); }else{ try{ var textDoc = self.m_XmlHttp.responseText; }catch( E ){ self.errorMSG( E.number+":"+E.description ); return false; } /* Do Something After Response */ self.doFuncHtml( textDoc ); } self.doFuncFinish(); } break; default : if( self.m_Aborted ){ window.clearTimeout( self.m_TimeOutID ); self.m_XmlHttp.abort(); self.debugMSG( "Time Out" ); self.doFuncFinish(); return false; } break; } };
( this.m_Method.toUpperCase() == "POST" ) ? this.doRequestUsingPost() : this.doRequestUsingGet();
} else { this.noXHR(); return; } };
this.noXHR = function(){ /* createXmlHttpRequest Error! */ this.debugMSG( this.m_CopyRight ); };
this.doFuncStart = function(){ this.debugMSG( "Before Send Request" ); document.getElementById("fade").innerHTML = "数据加载中,请稍候…"; document.getElementById("fade").style.display = "block"; };
this.doFuncFinish = function (){ this.debugMSG( "After do function Response" ); document.getElementById("fade").style.display = "none"; var objAs = document.getElementsByTagName("A"); var objA; for( var i=0; objA = objAs[i]; i++ ){ objA.onfocus = function(){ this.blur() }; } };
/* Must be reloaded */ this.doFuncXml = function( xmlDoc ){ if( xmlDoc != null ){ this.debugMSG( xmlDoc ); }else{ this.debugMSG( "Response NULL" ); } };
/* Must be reloaded */ this.doFuncHtml = function( textDoc ){ this.debugMSG( textDoc ); }; }; ?>
|  <?wzhu
wzhu.print("http://www.cncmm.com");
?> |
|