<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: xhconn code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 07 Oct 2008 12:14:04 GMT</pubDate>
    <description>DZone Snippets: xhconn code</description>
    <item>
      <title>Simple javascript XHR object with fix for missing onreadystatechange event in Firefox (for synchronous calls)</title>
      <link>http://snippets.dzone.com/posts/show/5416</link>
      <description>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/&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **&lt;br /&gt; ** Code licensed under Creative Commons Attribution-ShareAlike License      **&lt;br /&gt; ** http://creativecommons.org/licenses/by-sa/2.0/                           **/&lt;br /&gt;/**&lt;br /&gt; * Modified slightly from original to support synchoronous transactions&lt;br /&gt; * CDB 2007-05-16, 2007-06-05&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;function XHConn()&lt;br /&gt;{&lt;br /&gt;	var xmlhttp, bComplete = false;&lt;br /&gt;	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }&lt;br /&gt;	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }&lt;br /&gt;	catch (e) { try { xmlhttp = new XMLHttpRequest(); }&lt;br /&gt;	catch (e) { xmlhttp = false; }}}&lt;br /&gt;	if (!xmlhttp) return null;&lt;br /&gt;	this.connect = function(sURL, sMethod, sVars, fnDone, bAsynch)&lt;br /&gt;	{&lt;br /&gt;		if (!xmlhttp) return false;&lt;br /&gt;		bComplete = false;&lt;br /&gt;		sMethod = sMethod.toUpperCase();&lt;br /&gt;&lt;br /&gt;		if (bAsynch == null) bAsynch = true; //treat asynch as an optional argument&lt;br /&gt;&lt;br /&gt;		try {&lt;br /&gt;			if (sMethod == "GET")&lt;br /&gt;			{&lt;br /&gt;				xmlhttp.open(sMethod, sURL+"?"+sVars, (bAsynch == true));&lt;br /&gt;				sVars = "";&lt;br /&gt;			}&lt;br /&gt;			else&lt;br /&gt;			{&lt;br /&gt;				xmlhttp.open(sMethod, sURL, (bAsynch == true));&lt;br /&gt;				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");&lt;br /&gt;				xmlhttp.setRequestHeader("Content-Type",&lt;br /&gt;					"application/x-www-form-urlencoded");&lt;br /&gt;			}&lt;br /&gt;&lt;br /&gt;			xmlhttp.onreadystatechange = function(){&lt;br /&gt;				if (xmlhttp.readyState == 4 &amp;&amp; !bComplete)&lt;br /&gt;				{&lt;br /&gt;					bComplete = true;&lt;br /&gt;					fnDone(xmlhttp);&lt;br /&gt;				}&lt;br /&gt;			};&lt;br /&gt;			xmlhttp.send(sVars);&lt;br /&gt;&lt;br /&gt;			/**&lt;br /&gt;			 * Firefox &lt;= 2.0.0 doesn't fire onreadystatechange for synchronous requests.&lt;br /&gt;			 * See http://lukav.com/wordpress/2007/04/12/firefox-firebug-and-synchronos-calls-problem/&lt;br /&gt;			 */&lt;br /&gt;			var isGecko = (document.addEventListener) ? true : false;&lt;br /&gt;			try {&lt;br /&gt;				if (!bAsynch &amp;&amp; isGecko &amp;&amp; xmlhttp.onreadystatechange == null) {&lt;br /&gt;					bComplete = true;&lt;br /&gt;					fnDone(xmlhttp);&lt;br /&gt;					return true;&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;			catch (e) { return false; }&lt;br /&gt;			/**&lt;br /&gt;			 * End synchronous request patch&lt;br /&gt;			 */&lt;br /&gt;		}&lt;br /&gt;		catch(z) { return false; }&lt;br /&gt;		return true;&lt;br /&gt;	};&lt;br /&gt;	return this;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 21 Apr 2008 17:35:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5416</guid>
      <author>ChrisBloom7 (Chris Bloom)</author>
    </item>
  </channel>
</rss>
