<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Less code, more sleep &#187; jQuery</title>
	<atom:link href="http://emmett.be/thewebdev/tags/jquery/feed" rel="self" type="application/rss+xml" />
	<link>http://emmett.be/thewebdev</link>
	<description>Tales of an insomniac coder</description>
	<lastBuildDate>Mon, 26 Jul 2010 22:48:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Improved jquery ajax error reporting</title>
		<link>http://emmett.be/thewebdev/archives/64</link>
		<comments>http://emmett.be/thewebdev/archives/64#comments</comments>
		<pubDate>Sat, 12 Dec 2009 16:02:22 +0000</pubDate>
		<dc:creator>Emmett Pickerel</dc:creator>
				<category><![CDATA[Frontend]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://emmett.be/thewebdev/?p=64</guid>
		<description><![CDATA[jQuery is a great library, and I highly recommend it after 6 months of use. One of the problems I&#8217;ve had with it, however, is its poor error reporting on ajax requests. While you can subscribe to the global event ajaxError, there&#8217;s nothing to tell you what went wrong. Specifically, I&#8217;ve wanted a way to ]]></description>
			<content:encoded><![CDATA[<p>jQuery is a great library, and I highly recommend it after 6 months of use. One of the problems I&#8217;ve had with it, however, is its poor error reporting on ajax requests. While you can subscribe to the global event <code>ajaxError</code>, there&#8217;s nothing to tell you <em>what</em> went wrong. Specifically, I&#8217;ve wanted a way to report when the json parse fails.</p>
<p>Looking through the jQuery core, I found that the problem is that &#8220;status&#8221; field isn&#8217;t sent to the handler. A simple intervention on <code>jQuery.handleError</code> will close the gap:</p>
<pre>jQuery.handleErrorOriginal = $.handleError;
jQuery.handleError = function( s, xhr, status, e ){
  if (s.global) {
    xhr.error_status = status;
  }
  jQuery.handleErrorOriginal.apply(this, arguments);
};</pre>
<p>The &#8220;e&#8221; object is the original ajax configuration object. Since it reaches the global ajaxError listener, we can pull the status out of that.</p>
<p>Notice that I don&#8217;t <em>replace</em> the original functionality, I just do something before calling it with the same scope and arguments. The reason here is forwards-compatibility. Assuming that the name and api of jQuery.handleError doesn&#8217;t change, any changes to its internal functionality in future updates will likely be preserved without refactoring effort on your part.</p>
<p>To catch the errors:</p>
<pre>$("html").ajaxError(function(xhr, s, e){
    var msg;
    switch (s.error_status) {
      case "parsererror":
        msg = "A JSON parsing error occurred.";
        break;
      case "timeout":
        msg = "An ajax request timed out.";
        break;
      default:
        msg = "An ajax error occurred.";
        break;
    }
    alert(msg + "\n\n" + e.url);
});</pre>
<p>Why do I set <code>xhr.error_status</code> and then read <code>s.error_status</code>? Because, somewhere in the jumble of <code>handleError</code> and <code>trigger</code>, <code>xhr</code> becomes <code>s</code>. I love jQuery, but some of its core code can be quite difficult to read through.</p>
]]></content:encoded>
			<wfw:commentRss>http://emmett.be/thewebdev/archives/64/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery(document).ready() bug with Google Chrome 4.0 beta</title>
		<link>http://emmett.be/thewebdev/archives/46</link>
		<comments>http://emmett.be/thewebdev/archives/46#comments</comments>
		<pubDate>Thu, 19 Nov 2009 15:57:39 +0000</pubDate>
		<dc:creator>Emmett Pickerel</dc:creator>
				<category><![CDATA[Frontend]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[google chrome]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://emmett.be/thewebdev/?p=46</guid>
		<description><![CDATA[I came across a major issue with jQuery (1.3.2) and Google Chrome 4.0 beta today. I&#8217;d been testing my company&#8217;s web application (for which I&#8217;m the primary frontend developer) with Chrome 3.0x, and it was working fine. After a sudden influx of chrome bug reports from someone using 4, I updated to that. And suddenly, ]]></description>
			<content:encoded><![CDATA[<p>I came across a major issue with jQuery (1.3.2) and Google Chrome 4.0 beta today. I&#8217;d been testing my company&#8217;s web application (for which I&#8217;m the primary frontend developer) with Chrome 3.0x, and it was working fine. After a sudden influx of chrome bug reports from someone using 4, I updated to that. And suddenly, nearly nothing worked.</p>
<p>After a fair bit of futzing around in my code and the <a href="http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.3.2.js&amp;downloadBtn=" target="_blank">jQuery core library</a>, I discovered that in chrome 4 in certain cases (probably in large apps), the <code>DOMContentLoaded</code> event was never sent to the core jQuery library. To test this, I inserted a couple of lines into the file you saw above:</p>
<pre>3052  if ( document.addEventListener ) {
3053      // Use the handy event callback
<strong>[ins]     alert("jQ adding listener for DOMContentLoaded");</strong>
3054      document.addEventListener( "DOMContentLoaded", function(){
<strong>[ins]         alert("jQ received DOMContentLoaded event");</strong>
3055          document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
3056          jQuery.ready();
3057      }, false );</pre>
<p>Both alerts show in FF, but only the first does in chrome. However, I found I <em>could</em> add my own and have them called.</p>
<p>Now, since the chances are you&#8217;re on this page because you googled for a workaround, here it is:</p>
<p><a href="http://emmett.be/thewebdev/wp-content/uploads/2009/11/jquery.chrome4fix.js">Google Chrome 4 beta jQuery document ready fix plugin</a></p>
<p>What this plugin does is first test to see if the browser is chrome 4, and if it is, then it adds another event listener for DOMContentLoaded, which will run even though the main jQuery one mysteriously does not. This calls jQuery.ready function (which is what is used to run through jQuery.readyList, the queued functions array).</p>
<p>We now have two chances for jQuery.ready to be called, therefore a potential race condition. To avoid this, I rewrote jQuery.ready() to null out  jQuery.readyList <em>before</em> cycling through the queued functions.</p>
<p>I still have no idea what&#8217;s causing Chrome 4 to fail on this. A test of document.readyState right after I added $(document).ready() shows that it is &#8220;loading&#8221;, so the problem isn&#8217;t that it finishes before adding the event handler or enqueueing the function.</p>
]]></content:encoded>
			<wfw:commentRss>http://emmett.be/thewebdev/archives/46/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
