1. 论坛系统升级为Xenforo,欢迎大家测试!
    排除公告

vB 中的 AJAX 应用类

本帖由 不学无术2005-10-18 发布。版面名称:前端开发

  1. 不学无术

    不学无术 Ulysses 的元神

    注册:
    2005-08-31
    帖子:
    16,714
    赞:
    39
    我们只是借鉴它的程序思想,不算是侵权吧?:p

    代码:
    // #############################################################################
    // vB_AJAX_Handler
    // #############################################################################
    
    /**
    * XML Sender Class
    *
    * @param	boolean	Should connections be asyncronous?
    */
    function vB_AJAX_Handler(async)
    {
    	/**
    	* Should connections be asynchronous?
    	*
    	* @var	boolean
    	*/
    	this.async = async ? true : false;
    }
    
    // =============================================================================
    // vB_AJAX_Handler methods
    
    /**
    * Initializes the XML handler
    *
    * @return	boolean	True if handler created OK
    */
    vB_AJAX_Handler.prototype.init = function()
    {
    	try
    	{
    		this.handler = new XMLHttpRequest();
    		return (this.handler.setRequestHeader ? true : false);
    	}
    	catch(e)
    	{
    		try
    		{
    			this.handler = eval("new A" + "ctiv" + "eX" + "Ob" + "ject('Micr" + "osoft.XM" + "LHTTP');");
    			return true;
    		}
    		catch(e)
    		{
    			return false;
    		}
    	}
    }
    
    /**
    * Detects if the browser is fully compatible
    *
    * @return	boolean
    */
    vB_AJAX_Handler.prototype.is_compatible = function()
    {
    	if (is_ie && !is_ie4) { return true; }
    	else if (XMLHttpRequest)
    	{
    		try { return XMLHttpRequest.prototype.setRequestHeader ? true : false; }
    		catch(e)
    		{
    			try { var tester = new XMLHttpRequest(); return tester.setRequestHeader ? true : false; }
    			catch(e) { return false; }
    		}
    	}
    	else { return false; }
    }
    
    /**
    * Checks if the system is ready
    *
    * @return	boolean	False if ready
    */
    vB_AJAX_Handler.prototype.not_ready = function()
    {
    	return (this.handler.readyState && (this.handler.readyState < 4));
    }
    
    /**
    * OnReadyStateChange event handler
    *
    * @param	function
    */
    vB_AJAX_Handler.prototype.onreadystatechange = function(event)
    {
    	if (!this.handler)
    	{
    		if  (!this.init())
    		{
    			return false;
    		}
    	}
    	if (typeof event == 'function')
    	{
    		this.handler.onreadystatechange = event;
    	}
    	else
    	{
    		alert('XML Sender OnReadyState event is not a function');
    	}
    }
    
    /**
    * Sends data
    *
    * @param	string	Destination URL
    * @param	string	Request Data
    *
    * @return	mixed	Return message
    */
    vB_AJAX_Handler.prototype.send = function(desturl, datastream)
    {
    	if (!this.handler)
    	{
    		if (!this.init())
    		{
    			return false;
    		}
    	}
    	if (!this.not_ready())
    	{
    		this.handler.open('POST', desturl, this.async);
    		this.handler.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    		this.handler.send(datastream + '&s=' + fetch_sessionhash());
    
    		if (!this.async && this.handler.readyState == 4 && this.handler.status == 200)
    		{
    			return true;
    		}
    	}
    	return false;
    }
    
    // we can check this variable to see if browser is AJAX compatible
    var AJAX_Compatible = vB_AJAX_Handler.prototype.is_compatible();
    具体应用在下面的 JS 文件中:

    http://bbs.chinahtml.com/clientscript/vbulletin_ajax_threadslist.js