<?xml version="1.0" encoding="UTF-8"?>

<rss version='2.0'
     xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">
    <channel>
        <!-- This XML Feed shows details for the page Ajax-Development-Gotchas -->
        <creativeCommons:license>http://creativecommons.org/licenses/by-sa/2.5/
          </creativeCommons:license>
        <title>Ajax-Development-Gotchas</title>
        <description>&lt;p&gt;Gotchas in Ajax Development (&lt;strong&gt;please edit this list if you have a gotcha to contribute&lt;/strong&gt;)&lt;/p&gt;


	&lt;h2&gt;Basic Browser quirks and limitations&lt;/h2&gt;


&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;XMLHttpRequest can&amp;#8217;t access remote servers&lt;/strong&gt;. 

	&lt;p&gt;For some odd reason, although there are ways in javascript to send and receive data to remote servers, such as through script or iframe elements, &lt;em&gt;XMLHttpRequest cannot have a remote server as a target&lt;/em&gt;. This is a basic cross browser security rule that may not be immediately obvious to the newcomer to Ajax development.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multiple Ajax Requests are not fired in order&lt;/strong&gt;. 

	&lt;p&gt;IE, breaking from a long tradition of ignoring independent standards, chooses to follow the &lt;span class=&quot;caps&quot;&gt;HTTP 1&lt;/span&gt;.1 &lt;a rel=&quot;nofollow&quot; href=&quot;http://www.faqs.org/rfcs/rfc2616.html&quot;&gt;&lt;span class=&quot;caps&quot;&gt;RFC 2616&lt;/span&gt;&lt;/a&gt; to the letter, which means that IE may only have two XMLHttpRequests open at a time; after which IE will retain an internal queue of requests &lt;em&gt;which will be serviced in no particular order&lt;/em&gt;. Even if the requests are fired in order, the nature of the internet dictates that they will not be received in order, so never write code that assumes XMLHttpRequests will be sent in a particular order.&lt;/p&gt;


&lt;ul&gt;
  &lt;li&gt;

	&lt;p&gt;Firefox also has a similar albeit more liberal limitation in the number of simultaneous XMLHttpRequests that may be open; however in Firefox 1.5, the developer may &lt;a rel=&quot;nofollow&quot; href=&quot;http://developer.mozilla.org/en/docs/Changing_the_Priority_of_HTTP_Requests&quot;&gt;modify the priority&lt;/a&gt; of the internal request queue.&lt;/p&gt;


  &lt;/li&gt;
&lt;/ul&gt;

&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Asynchronous XMLHttpRequests responses will arrive in no particular order&lt;/strong&gt;. 

	&lt;p&gt;As implied by the word Asynchronous, XMLHttpRequest responses may arrive at any time in an unpredictable order that is ignorant of developer intent, happily executing callbacks in the random order in which they eventually wind up on the client.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;XMLHttpRequest does not require the use of &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt;&lt;/strong&gt;. 

	&lt;p&gt;While Ajax is aptly named when it comes to asynchronous execution, it is poorly named when it comes to &lt;a class=&quot;wikilink&quot; href=&quot;http://swik.net/XML&quot;&gt;&lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt;&lt;/a&gt;. The &lt;a class=&quot;wikilink&quot; href=&quot;http://swik.net/XMLHttpRequest&quot;&gt;XMLHttpRequest&lt;/a&gt; object has a useful method called responseText that delivers the straight text of the response, be it &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt;, &lt;a class=&quot;wikilink&quot; href=&quot;http://swik.net/JSON&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JSON&lt;/span&gt;&lt;/a&gt; or just simple unadorned text. Using &lt;span class=&quot;caps&quot;&gt;JSON&lt;/span&gt; or simple text can be much faster, easier, and more concise than &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt;.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ajax uses &lt;span class=&quot;caps&quot;&gt;UTF&lt;/span&gt;-8&lt;/strong&gt;. 

	&lt;p&gt;Normal forms are sent using the encoding of the parent page. Thus a &lt;span class=&quot;caps&quot;&gt;SJIS&lt;/span&gt; encoded page will default to sending form content encoded in &lt;span class=&quot;caps&quot;&gt;SJIS&lt;/span&gt;. Ajax submitted forms on the other hand will be sent as &lt;span class=&quot;caps&quot;&gt;UTF&lt;/span&gt;-8. If for some strange reason, &lt;span class=&quot;caps&quot;&gt;UTF&lt;/span&gt;-8 is not the character set of choice for the server, this will require a solution such as the server recognizing and translating &lt;span class=&quot;caps&quot;&gt;UTF&lt;/span&gt;-8 responses to a desired character encoding.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ajax requests are url encoded&lt;/strong&gt;. 

	&lt;p&gt;A bug relating to this can currently be seen on &lt;a class=&quot;wikilink&quot; href=&quot;http://swik.net/Digg&quot;&gt;Digg.com&lt;/a&gt;. Submit a comment to digg, and then edit the comment to include a unicode character. The updated comment, sent via Ajax will be added to the database as the url encoded string: resulting in an odd looking url encoded comment.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;XMLHttpRequest cannot transmit files&lt;/strong&gt;. 

	&lt;p&gt;In a javascript heavy application, a developer might need to include the ability to send files without completely refreshing the page. XMLHttpRequest however does not include this ability. Instead, a hidden iframe can be used to send the file, and Javascript can simply inspect the iframe. Ajax techniques can further complement this, for example the open source project &lt;a class=&quot;wikilink&quot; href=&quot;http://swik.net/Uber-Uploader&quot;&gt;Uber-Uploader&lt;/a&gt; can show a progress bar while uploading a file.&lt;/p&gt;


&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;Ajax data transfer is text based&lt;/strong&gt;&lt;/li&gt;

	&lt;p&gt;If you are using Javascript to create data outside of text encoding, and you try to send it through over an XMLHttpRequest, you may notice corruption in your transmission as some characters are not transmitted properly. To solve this, use the native JavaScript escape() before using the encodeURIComponent function, then use the unescape function when the data comes back later.&lt;/p&gt;


&lt;/ol&gt;

	&lt;h2&gt;&lt;a class=&quot;wikilink&quot; href=&quot;http://swik.net/Firefox&quot;&gt;Firefox&lt;/a&gt; quirks or bugs&lt;/h2&gt;


&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Synchronous XMLHttpRequests lock up Firefox&lt;/strong&gt;. 

	&lt;p&gt;The XMLHttpRequest method provides a third argument, which defines whether the Ajax requests are submitted synchronously or asynchronously. (&lt;span style=&quot;font-family:monospace;&quot;&gt;xmlhttprequest.open(&amp;#8216;GET&amp;#8217;, &amp;#8216;http://www.mozilla.org/&amp;#8217;, &lt;strong&gt;false&lt;/strong&gt;);&lt;/span&gt;) On Firefox, setting this value to false will submit the xmlhttprequest synchronously and lock up the entire browser for the duration of the request.&lt;/p&gt;


&lt;/li&gt;
&lt;/ol&gt;

	&lt;h2&gt;IE quirks or bugs&lt;/h2&gt;


&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;XMLHttpRequest Objects are not reused in IE&lt;/strong&gt;. 

	&lt;p&gt;Unless the abort method is used, XMLHttpRequest objects in IE won&amp;#8217;t function after the first use, unlike in other browsers such as Firefox. One solution to this is to create a new XMLHttpRequest object for each connection. Some fear this causes memory leaks, but there seems to be no conclusive proof of this as long as the known closures memory leak issue is avoided (detailed later).&lt;/p&gt;


	&lt;p&gt;Another solution to this issue is to reverse the order of the callback handler code in Javascript. More info on this solution can be found in the article &lt;a rel=&quot;nofollow&quot; href=&quot;http://keelypavan.blogspot.com/2006/03/reusing-xmlhttprequest-object-in-ie.html&quot;&gt;Reusing XMLHttpRequest object in IE&lt;/a&gt;&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IE doesn&amp;#8217;t use cached images when Javascript inserts &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; with images&lt;/strong&gt;.

	&lt;p&gt;This is a general JavaScript behavior, but with Ajax heavy applications that insert a lot of &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; snippets, this can lead to a lot of unnecessary image redownloading. The way to solve this is to send all images with proper Cache-Control, ETag, and Last-Modified headers. IE will request images with a &amp;#8216;If-Modified-Since&amp;#8217; header and the server should send back a 304 header which will tell IE not to redownload the image, and use its cached image.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Closures with circular references in IE cause memory leaks&lt;/strong&gt;. 

	&lt;p&gt;It&amp;#8217;s common in Ajax to use closures, however in IE if a closure has a circular reference, it will cause IE to start leaking memory. In Javascript it&amp;#8217;s easy to create closures with circular references, so care should be taken to avoid memory leaks.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoiding aggressive caching in IE&lt;/strong&gt; 

	&lt;p&gt;Even with proper &amp;#8216;no-cache&amp;#8217; headers, IE caches &amp;#8216;GET&amp;#8217; requests &amp;#8211; this can be solved by changing the get request query string with every request.&lt;/p&gt;


	&lt;p&gt;An alternative solution for this is to use &amp;#8220;If-Modified-Since&amp;#8221; request header to point to some past date using xhr.setRequestHeader(&amp;#8220;If-Modified-Since&amp;#8221;,&amp;#8221;some past date&amp;#8221;); If you can&amp;#8217;t modify the query string, this is a workaround that will stop IE from caching too aggressively.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IE corrupts gzipped JavaScript files&lt;/strong&gt;. 

	&lt;p&gt;Although this is a more general issue with IE&amp;#8217;s handling of gzipped content, in Ajax applications where there is a lot of JavaScript, developers may be tempted to gzip their JavaScript to allow for faster page loading and lowered bandwidth consumption. However some versions of IE have a known bug where downloads are cut off mid-file, corrupting a downloading gzipped JavaScript file.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IE doesn&amp;#8217;t cache gzipped JavaScript files&lt;/strong&gt;. 

	&lt;p&gt;Adding to the poor handling of gzipped content in some versions of IE is IE&amp;#8217;s lack of support for simultaneous gzip and &lt;a rel=&quot;nofollow&quot; href=&quot;http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html&quot;&gt;ETag&lt;/a&gt; cache headers. Again, this is wider scope than simply JavaScript, but it means that even if IE didn&amp;#8217;t corrupt the gzipped JavaScript, it wouldn&amp;#8217;t cache it anyways.&lt;/p&gt;


&lt;/li&gt;
&lt;/ol&gt;

	&lt;p&gt;&lt;a style=&quot;margin-left:-450px;&quot; href=&quot;http://petbald.ru&quot;&gt;Cats peterbald&lt;/a&gt;&lt;/p&gt;
</description>
                <category>Development</category>
        <category>Ajax</category>
        <category>Gotchas</category>

        <pubDate>Sun, 30 Apr 2006 23:02:54 -0700</pubDate>
        <lastBuildDate>Fri, 18 Apr 2008 06:41:37 -0700</lastBuildDate>
            
    </channel>
</rss>
