喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 喜悦证交所 | 帮助

RSS 订阅当前论坛  

招聘PHP开发人员(北京)

上一主题 下一主题
     
标题: [代码] 欢迎优化、指教( Ajax )  
 
wzhu (wzhu)
版主
Rank: 7Rank: 7Rank: 7
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_TimeOutIDnull;  
    
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" ){
            
alertcontent this.m_CopyRight );
        }
    };

    
this.warnMSG = function( content ){
        if( 
this.m_AlertLevel != "E" ){
            
alertcontent this.m_CopyRight );
        }
    };

    
this.errorMSG = function( content ){
        
alertcontent 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( 
){
                
this.m_XmlHttp false;
                
this.warnMSGE.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( namevalue ){
        
this.m_QueryString += name "=" encodeURIComponentvalue ) + "&";
    };

    
this.doRequestUsingPost = function() {
        if( !
this.m_IsCache ){
            
this.addTimeStamp();
        }    
        
this.m_XmlHttp.openthis.m_Methodthis.m_RequestUrlthis.m_IsAsync );
        
//this.m_XmlHttp.open( this.m_Method, this.m_RequestUrl );
        
this.setCommonHeader();
        
this.debugMSGthis.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.sendthis.m_QueryString );
    };

    
this.doRequestUsingGet = function() {
        var 
queryString;
        if( !
this.m_IsCache ){
            
this.addTimeStamp();
        }        
        if( 
this.m_RequestUrl.indexOf("?") > ){
            
queryString this.m_RequestUrl "&" this.m_QueryString;
        } else {
            
queryString this.m_RequestUrl "?" this.m_QueryString;
        }

        
this.debugMSGqueryString );
        
this.m_XmlHttp.openthis.m_MethodqueryStringthis.m_IsAsync );
        
this.setCommonHeader();
        
this.m_XmlHttp.sendnull );
    };

    
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.debugMSGself.m_XmlHttp.readyState );
                switch ( 
self.m_XmlHttp.readyState ) {
                    case 
:
                        
window.clearTimeoutself.m_TimeOutID );
                        if( 
self.m_Aborted ){
                            
self.m_XmlHttp.abort();
                            
self.warnMSG"TimeOut" );
                            
self.doFuncFinish();
                            return 
false;
                        }
                        if( 
self.m_XmlHttp.status == 200 ) {
                            
self.debugMSGself.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 
0MSXMLDOMS.lengthi++ ) {
                                                try {
                                                    
self.debugMSG (MSXMLDOMS[i] );
                                                    
xmlDom = new ActiveXObjectMSXMLDOMS[i] );    break;                            
                                                }catch( 
){
                                                    
self.warnMSG(  E.number+":"+E.description  );
                                                }
                                            }
                                            
xmlDom.validateOnParse true;
                                            if( !
xmlDom.loadXMLself.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.parseFromStringself.m_XmlHttp.responseText,"text/xml");   
                                            if ( 
xmlDom.documentElement.tagName == "parsererror" ) {
                                                throw 
"XML DOM parsing failed";// + xmlDom.documentElement;
                                            
}
                                        }                                        
                                    }
                                    
//*/
                                
}catch( ){
                                    
self.errorMSG);
                                    
self.doFuncFinish();
                                    return 
false;

                                }
                                
/*  Do Something After Response  */
                                
self.doFuncXmlxmlDom );
                            }else{
                                try{
                                    var 
textDoc =  self.m_XmlHttp.responseText;
                                }catch( 
){
                                    
self.errorMSGE.number+":"+E.description );
                                    return 
false;
                                }                                
                                
/*  Do Something After Response  */
                                
self.doFuncHtmltextDoc );
                            }
                            
self.doFuncFinish();
                        }
                        break;
                    default :
                        if( 
self.m_Aborted ){
                            
window.clearTimeoutself.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.debugMSGthis.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=0objA objAs[i]; i++ ){
            
objA.onfocus = function(){ this.blur() };
        }        
    };

    
/*  Must be reloaded  */
    
this.doFuncXml = function( xmlDoc ){
        if( 
xmlDoc != null ){
            
this.debugMSGxmlDoc );
        }else{
            
this.debugMSG"Response NULL" );
        }
    };

    
/*  Must be reloaded  */
    
this.doFuncHtml = function( textDoc ){
        
this.debugMSGtextDoc );
    };
};
?>




<?wzhu
wzhu.print("http://www.cncmm.com");
?>
2007-6-15 02:51 PM#1
查看资料  访问主页  Blog  发短消息  QQ  顶部
 
looking_for1
注册会员
Rank: 2


UID 78393
精华 0
积分 112
帖子 304
金钱 112 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2006-8-15
来自 hubei
状态 离线
[广告]: 代充Paypal帐号美元
php?

php里面怎么可以直接写js,你做过处理了吗?



爱我的人们,我一定会爱你们
2007-6-18 02:04 PM#2
查看资料  访问主页  Blog  发短消息  QQ  Yahoo!  顶部
 
wzhu (wzhu)
版主
Rank: 7Rank: 7Rank: 7
C[A-Z]O


UID 68040
精华 0
积分 47793
帖子 1569
金钱 47770 喜悦币
威望 0
人脉 23
阅读权限 100
注册 2006-1-14
来自 湖北荆州>陕西西安>辽宁 ...
状态 离线
[推荐阅读] 求一条SQL语句
当然是为了给大家一个语法亮度,好看一点。
请不要误解,另外里面有部分溢出字符被转换了。请注意。




<?wzhu
wzhu.print("http://www.cncmm.com");
?>
2007-6-18 02:50 PM#3
查看资料  访问主页  Blog  发短消息  QQ  顶部
 
熟人
中级会员
Rank: 3Rank: 3



UID 92185
精华 0
积分 332
帖子 476
金钱 332 喜悦币
威望 0
人脉 0
阅读权限 30
注册 2007-4-29
状态 离线
[推荐阅读] 想问大家碰到新中大这样的问题吗?
回复 #3 wzhu 的帖子

呵呵 js用php的高亮规则并不是很合适 况且村里的php标签会吃斜杠



性格:馋 懒 奸 猾 坏
兴趣:吃 喝 嫖 赌 抽
职业:坑 蒙 拐 骗 偷
2007-6-18 04:10 PM#4
查看资料  发短消息  顶部
 
wzhu (wzhu)
版主
Rank: 7Rank: 7Rank: 7
C[A-Z]O


UID 68040
精华 0
积分 47793
帖子 1569
金钱 47770 喜悦币
威望 0
人脉 23
阅读权限 100
注册 2006-1-14
来自 湖北荆州>陕西西安>辽宁 ...
状态 离线
[推荐阅读] 三个月后跳槽。


QUOTE:
原帖由 熟人 于 2007-6-18 16:10 发表
呵呵 js用php的高亮规则并不是很合适 况且村里的php标签会吃斜杠
我也知道,不过相较一片白底黑字的还是要稍微好一点,不过吃斜杠确实要提醒一下。




<?wzhu
wzhu.print("http://www.cncmm.com");
?>
2007-6-18 04:18 PM#5
查看资料  访问主页  Blog  发短消息  QQ  顶部
 
looking_for1
注册会员
Rank: 2


UID 78393
精华 0
积分 112
帖子 304
金钱 112 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2006-8-15
来自 hubei
状态 离线
[推荐阅读] 时间不能调用了
狂晕,头尾加上<?php ?>原来是为了显示高亮,真是晕死,:|)



爱我的人们,我一定会爱你们
2007-6-20 09:13 AM#6
查看资料  访问主页  Blog  发短消息  QQ  Yahoo!  顶部
 
looking_for1
注册会员
Rank: 2


UID 78393
精华 0
积分 112
帖子 304
金钱 112 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2006-8-15
来自 hubei
状态 离线
[推荐阅读] PHP批量添加记录到数据库
找到帮助里面去了,



爱我的人们,我一定会爱你们
2007-6-20 09:19 AM#7
查看资料  访问主页  Blog  发短消息  QQ  Yahoo!  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.065644 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP