<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Five 's Weblog &#187; Ajax</title>
	<atom:link href="http://powerdream5.wordpress.com/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://powerdream5.wordpress.com</link>
	<description>It is not just another blog! 伍行天下！</description>
	<lastBuildDate>Wed, 28 Nov 2007 21:21:29 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='powerdream5.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/b83f3cb0955eab114902a009525db137?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Five 's Weblog &#187; Ajax</title>
		<link>http://powerdream5.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://powerdream5.wordpress.com/osd.xml" title="Five &#8217;s Weblog" />
		<item>
		<title>Ajax, IE and Firefox!</title>
		<link>http://powerdream5.wordpress.com/2007/10/11/ajax-ie-and-firefox/</link>
		<comments>http://powerdream5.wordpress.com/2007/10/11/ajax-ie-and-firefox/#comments</comments>
		<pubDate>Thu, 11 Oct 2007 01:42:24 +0000</pubDate>
		<dc:creator>powerdream5</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[Ie]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://powerdream5.wordpress.com/2007/10/11/ajax-ie-and-firefox/</guid>
		<description><![CDATA[       As a web programmer, I believe that you must have complained the differences between IE and firefox. Sometimes, the web you designed looks perfect in one web browser, but looks ugly in other browsers. Furthermore, because IE and Firefox parse javascript in different ways, when we use the technology of ajax, sometimes, we have [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=powerdream5.wordpress.com&blog=1840509&post=39&subd=powerdream5&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://powerdream5.files.wordpress.com/2007/10/10_1_2007.JPG" title="10_1_2007.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_2_2007.JPG" title="10_2_2007.JPG"></a>       As a web programmer, I believe that you must have complained the differences between IE and firefox. Sometimes, the web you designed looks perfect in one web browser, but looks ugly in other browsers. Furthermore, because IE and Firefox parse javascript in different ways, when we use the technology of ajax, sometimes, we have to program for IE and firefox separately.<br />
      Today, I am going to show an example of using javascript to parse XML files in both IE and firefox, so the the application would behave the same in both of the web browsers.</p>
<p>      The interface of the example looks like:<br />
<a href="http://powerdream5.files.wordpress.com/2007/10/10_1_2007.JPG" title="10_1_2007.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_1_2007.JPG" title="10_1_2007.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_1_2007.JPG" title="10_1_2007.JPG"></a></p>
<p style="text-align:center;"><img src="http://powerdream5.files.wordpress.com/2007/10/10_1_2007.JPG" alt="10_1_2007.JPG" /></p>
<p>        Each time, after selecting a country from the drop list of country, a request will be sent to the server for the xml files. For this example, I prepare two xml files, china.xml and united_state.xml separately. Then, the client will get the response from the server, and it will parse the xml file and display the cities of the coutry in the list under the lable of city.</p>
<p><font color="#ff0000">//china.xml<br />
</font>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;gb2312&#8243;?&gt;<br />
&lt;cities&gt;<br />
      &lt;city&gt;Beijing&lt;/city&gt;<br />
      &lt;city&gt;Shanghai&lt;/city&gt;<br />
      &lt;city&gt;Chengdu&lt;/city&gt;<br />
      &lt;city&gt;Wuhan&lt;/city&gt;<br />
      &lt;city&gt;Changsha&lt;/city&gt;<br />
&lt;/cities&gt;</p>
<p><font color="#ff0000">//united_state.xml</font><br />
&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;gb2312&#8243;?&gt;<br />
&lt;cities&gt;<br />
      &lt;city&gt;New York&lt;/city&gt;<br />
      &lt;city&gt;Sanfrancisco&lt;/city&gt;<br />
      &lt;city&gt;Los Angeles&lt;/city&gt;<br />
      &lt;city&gt;Philadelphia&lt;/city&gt;<br />
      &lt;city&gt;Chicago&lt;/city&gt;<br />
      &lt;city&gt;Seattle&lt;/city&gt;<br />
      &lt;city&gt;Houston&lt;/city&gt;<br />
&lt;/cities&gt;</p>
<p>The core javascript code are showed below:</p>
<p>    <font color="#ff0000">//create the ajax coonnection<br />
    //for IE</font><br />
<font color="#ff0000">    <font color="#000000">var req = null;</font><br />
</font>    if(window.ActiveXObject)<br />
    {<br />
          req = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
    }<br />
    <font color="#ff0000">//for Firefox<br />
</font>    else if(window.XMLHttpRequest)<br />
    {<br />
          req = new XMLHttpRequest();<br />
    }<br />
    <font color="#ff0000">//define the file requested<br />
</font>    var url = document.form1.country.options[selected].value+&#8221;.xml&#8221;;<br />
    <font color="#ff0000">//define which function to deal with the result of the request</font><br />
    req.onreadystatechange = getCities;<br />
    <font color="#ff0000">//using Post or get method to send the request,we use get method here</font><br />
    req.open(&#8220;GET&#8221;,url, true);<br />
    <font color="#ff0000">//send request to the server<br />
    //there is a trick, when you don&#8217;t want to send any parameters<br />
    //you need to use null, otherwise, there will be some problem in Firefox</font><br />
       req.send(null);</p>
<p>function getCities(){<br />
   <font color="#ff0000">//when the request is complete</font><br />
   if(req.readyState == 4){<br />
       <font color="#ff0000">//check if the request is success<br />
</font>      if(req.status == 200){<br />
           <font color="#ff0000"> //req.responseText represents the returning text from the server<br />
</font>           var response = req.responseText;<br />
           var xmlDoc;<br />
           var value;<br />
           document.form1.city.options.length = 0;<br />
          <font color="#ff0000">//for IE<br />
</font>         if (window.ActiveXObject)<br />
        {<br />
             <font color="#ff0000">//crate xml docuemnt object in IE</font><br />
            xmlDoc=new ActiveXObject(&#8220;Microsoft.XMLDOM&#8221;);<br />
            xmlDoc.async=&#8221;false&#8221;;<br />
            xmlDoc.loadXML(response);<br />
            var x = xmlDoc.documentElement;<br />
            <font color="#ff0000">//navigate the xml docuemnt object<br />
            //take notice: firstly, we assign 0 to i, then i adds 1 each time</font><br />
           for (var i=0;i&lt;x.childNodes.length;(i = i+1))<br />
          {<br />
                 value = x.childNodes[i].childNodes[0].nodeValue;<br />
                 <font color="#ff0000">//add the city to the list one by one<br />
</font>                 document.form1.city.options[i] = new Option(value,value);<br />
          }    <br />
       }<br />
       <font color="#ff0000"> //for Firefox</font><br />
       else<br />
       {<br />
           <font color="#ff0000">//create the xml docuemnt object in firefox<br />
</font>           var parser=new DOMParser();<br />
           xmlDoc=parser.parseFromString(response,&#8221;text/xml&#8221;);<br />
           var x = xmlDoc.documentElement;<br />
           var num = 0;<br />
           <font color="#ff0000">//take notice: firstly, we assign 1 to i, then i adds 2 each time<br />
</font>           for (var i=1;i&lt;x.childNodes.length;(i = i+2))<br />
           {<br />
                 value = x.childNodes[i].childNodes[0].nodeValue;<br />
                 <font color="#ff0000">//add the city to the list one by one</font><br />
                 document.form1.city.options[num] = new Option(value,value);<br />
                 num++;<br />
            }   <br />
         }<br />
       }<br />
     }<br />
  }</p>
<p>The html code is listed below:</p>
<p>&lt;form name=&#8221;form1&#8243;&gt;<br />
     &lt;table width=&#8221;300&#8243; border=&#8221;0&#8243;&gt;<br />
          &lt;tr&gt;<br />
              &lt;td width=&#8221;150&#8243; align=&#8221;center&#8221;&gt;Country&lt;/td&gt;<br />
              &lt;td width=&#8221;150&#8243; align=&#8221;center&#8221;&gt;City&lt;/td&gt;<br />
          &lt;/tr&gt;<br />
          &lt;tr&gt;<br />
              &lt;td valign=&#8221;top&#8221; align=&#8221;center&#8221;&gt;<br />
                    &lt;select name=&#8221;country&#8221; onchange=&#8221;getTheCity()&#8221;&gt;<br />
                         &lt;option&gt;&lt;/option&gt;<br />
                         &lt;option value=&#8221;china&#8221;&gt;China&lt;/option&gt;<br />
                         &lt;option value=&#8221;united_state&#8221;&gt;United State&lt;/option&gt;<br />
                    &lt;/select&gt;<br />
              &lt;/td&gt;<br />
               &lt;td&gt;<br />
                     &lt;select name=&#8221;city&#8221; size=&#8221;10&#8243; style=&#8221;width:140px&#8221;&gt;<br />
                     &lt;/select&gt;<br />
               &lt;/td&gt;<br />
          &lt;/tr&gt;<br />
     &lt;/table&gt;<br />
 &lt;/form&gt;</p>
<p>You can see the perfomance of this example through <a target="_blank" href="http://free.hostultra.com/~powerdream5/ajax/ajax.html">this link</a></p>
<p><a href="http://powerdream5.files.wordpress.com/2007/10/10_2_2007.JPG" title="10_2_2007.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_2_2007.JPG" title="10_2_2007.JPG"></p>
<p style="text-align:center;"><img src="http://powerdream5.files.wordpress.com/2007/10/10_2_2007.JPG" alt="10_2_2007.JPG" /></p>
<p></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/powerdream5.wordpress.com/39/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/powerdream5.wordpress.com/39/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/powerdream5.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/powerdream5.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/powerdream5.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/powerdream5.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/powerdream5.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/powerdream5.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/powerdream5.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/powerdream5.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/powerdream5.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/powerdream5.wordpress.com/39/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=powerdream5.wordpress.com&blog=1840509&post=39&subd=powerdream5&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://powerdream5.wordpress.com/2007/10/11/ajax-ie-and-firefox/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a0819a06b6ff4afc4f3e0a1977e6a5a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">powerdream5</media:title>
		</media:content>

		<media:content url="http://powerdream5.files.wordpress.com/2007/10/10_1_2007.JPG" medium="image">
			<media:title type="html">10_1_2007.JPG</media:title>
		</media:content>

		<media:content url="http://powerdream5.files.wordpress.com/2007/10/10_2_2007.JPG" medium="image">
			<media:title type="html">10_2_2007.JPG</media:title>
		</media:content>
	</item>
	</channel>
</rss>