Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Simple javascript XHR object with fix for missing onreadystatechange event in Firefox (for synchronous calls)

This code fixes the issue in Firefox where the onreadystatechange event is not called for synchronous XHR requests. It is based on the XHConn script from http://xkr.us/code/javascript/XHConn/, patched with the fix documented at http://lukav.com/wordpress/2007/04/12/firefox-firebug-and-synchronos-calls-problem/

   1  
   2  /** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
   3   ** Code licensed under Creative Commons Attribution-ShareAlike License      **
   4   ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
   5  /**
   6   * Modified slightly from original to support synchoronous transactions
   7   * CDB 2007-05-16, 2007-06-05
   8   */
   9  
  10  function XHConn()
  11  {
  12  	var xmlhttp, bComplete = false;
  13  	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  14  	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  15  	catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  16  	catch (e) { xmlhttp = false; }}}
  17  	if (!xmlhttp) return null;
  18  	this.connect = function(sURL, sMethod, sVars, fnDone, bAsynch)
  19  	{
  20  		if (!xmlhttp) return false;
  21  		bComplete = false;
  22  		sMethod = sMethod.toUpperCase();
  23  
  24  		if (bAsynch == null) bAsynch = true; //treat asynch as an optional argument
  25  
  26  		try {
  27  			if (sMethod == "GET")
  28  			{
  29  				xmlhttp.open(sMethod, sURL+"?"+sVars, (bAsynch == true));
  30  				sVars = "";
  31  			}
  32  			else
  33  			{
  34  				xmlhttp.open(sMethod, sURL, (bAsynch == true));
  35  				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
  36  				xmlhttp.setRequestHeader("Content-Type",
  37  					"application/x-www-form-urlencoded");
  38  			}
  39  
  40  			xmlhttp.onreadystatechange = function(){
  41  				if (xmlhttp.readyState == 4 && !bComplete)
  42  				{
  43  					bComplete = true;
  44  					fnDone(xmlhttp);
  45  				}
  46  			};
  47  			xmlhttp.send(sVars);
  48  
  49  			/**
  50  			 * Firefox <= 2.0.0 doesn't fire onreadystatechange for synchronous requests.
  51  			 * See http://lukav.com/wordpress/2007/04/12/firefox-firebug-and-synchronos-calls-problem/
  52  			 */
  53  			var isGecko = (document.addEventListener) ? true : false;
  54  			try {
  55  				if (!bAsynch && isGecko && xmlhttp.onreadystatechange == null) {
  56  					bComplete = true;
  57  					fnDone(xmlhttp);
  58  					return true;
  59  				}
  60  			}
  61  			catch (e) { return false; }
  62  			/**
  63  			 * End synchronous request patch
  64  			 */
  65  		}
  66  		catch(z) { return false; }
  67  		return true;
  68  	};
  69  	return this;
  70  }

Synchronous AJAX

// Snippet showing how to use synchronous AJAX

   1  
   2  function getFile(url) {
   3    if (window.XMLHttpRequest) {              
   4      AJAX=new XMLHttpRequest();              
   5    } else {                                  
   6      AJAX=new ActiveXObject("Microsoft.XMLHTTP");
   7    }
   8    if (AJAX) {
   9       AJAX.open("GET", url, false);                             
  10       AJAX.send(null);
  11       return AJAX.responseText;                                         
  12    } else {
  13       return false;
  14    }                                             
  15  }
  16  
  17  var fileFromServer = getFile('http://somedomain.com/somefile.txt');
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS