<?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>Remember the code? &#187; sarah</title>
	<atom:link href="http://code.neox.net/tag/sarah/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.neox.net</link>
	<description>a place where I store my secret powerful coding snippets, or just to remember how to do things</description>
	<lastBuildDate>Fri, 19 Jun 2009 11:36:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SarahAjax Javascript Ajax Class</title>
		<link>http://code.neox.net/2008/06/03/sarahajax-javascript-ajax-class/</link>
		<comments>http://code.neox.net/2008/06/03/sarahajax-javascript-ajax-class/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 03:17:17 +0000</pubDate>
		<dc:creator>HanaDaddy</dc:creator>
				<category><![CDATA[Javascript and CSS]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[sarah]]></category>

		<guid isPermaLink="false">http://code.neox.net/?p=34</guid>
		<description><![CDATA[Download sarahajax.zip
Okay, here is my SarahAjax Javascript Class. You can use it without hassle!
You can either inherit and &#8216;action&#8217; function or by prototyping a function named &#8216;action&#8217;. &#8216;action&#8217; function will be called when the request to the resource is successful.
This is SarahAjax Class.

// 6/2/2008 HanaDaddy  hanadaddy at gmail.com
&#160;
/* SarahAjax is a very simple ajax [...]]]></description>
			<content:encoded><![CDATA[<p><a href='/sarahajax.zip'><strong>Download sarahajax.zip</strong></a></p>
<p>Okay, here is my SarahAjax Javascript Class. You can use it without hassle!</p>
<p>You can either inherit and &#8216;action&#8217; function or by prototyping a function named &#8216;action&#8217;. &#8216;action&#8217; function will be called when the request to the resource is successful.</p>
<p>This is SarahAjax Class.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #009900; font-style: italic;">// 6/2/2008 HanaDaddy  hanadaddy at gmail.com</span>
&nbsp;
<span style="color: #009900; font-style: italic;">/* SarahAjax is a very simple ajax class.
 * url: target url to call. It is usally dynamic script or a simple page return xml or text.
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> SarahAjax<span style="color: #66cc66;">&#40;</span>url<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">url</span>=url;
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">reqobj</span> ; 
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">method</span>=<span style="color: #3366CC;">&quot;GET&quot;</span>;
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">async</span>= <span style="color: #003366; font-weight: bold;">true</span>; <span style="color: #009900; font-style: italic;">//if false , the connection will be synchronous ( you have to wait for the reply)</span>
	<span style="color: #009900; font-style: italic;">//this.action = function (obj) { }  // Success reply handler. : youre responsibilty </span>
	<span style="color: #009900; font-style: italic;">//this.error  = function (obj) { }  // Error reply handler : your responsiblity to implement.	</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">sendRequest</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">reqobj</span> == <span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>window.<span style="color: #006600;">XMLHttpRequest</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">reqobj</span> = <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    			<span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>window.<span style="color: #006600;">ActiveXObject</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">reqobj</span> = <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;Microsoft.XMLHTTP&quot;</span><span style="color: #66cc66;">&#41;</span>;
    			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">action</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;SaraAjax:Action member function is not defined! We need the reply handler!&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000066; font-weight: bold;">return</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">reqobj</span>.<span style="color: #000066;">open</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">method</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">url</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">async</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> sajaxreq = <span style="color: #000066; font-weight: bold;">this</span>; <span style="color: #009900; font-style: italic;">// this is necessary to access myself in the callback function</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">reqobj</span>.<span style="color: #006600;">onreadystatechange</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> sajaxreq.<span style="color: #006600;">callback</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">reqobj</span>.<span style="color: #006600;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">callback</span> = <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">reqobj</span>.<span style="color: #006600;">readyState</span> == <span style="color: #CC0000;">4</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">reqobj</span>.<span style="color: #000066;">status</span> == <span style="color: #CC0000;">200</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #009900; font-style: italic;">//alert(&quot;reply is back?&quot;+this.reqobj.responseText);</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">action</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">reqobj</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #66cc66;">&#123;</span>
			 	<span style="color: #009900; font-style: italic;">//alert(&quot;There was a problem retrieving data:\n&quot; + this.reqobj.statusText);</span>
			 	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">error</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			 		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">error</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">reqobj</span><span style="color: #66cc66;">&#41;</span>;
			 	<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>		
	<span style="color: #66cc66;">&#125;</span>	
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><span id="more-34"></span><br />
<a href='/sarahajax/usage1.html'><strong>Click here for Usage Example1: Prototyping</strong></a><br />
How to use: </p>
<ol>
<li>Include the script <code>sarahajax.js</code> file in your html file.</li>
<li>Implement &#8216;<code>action</code>&#8216; function by prototyping. This function is only called when request reply is OK.</li>
<li>Optionally add &#8216;<code>error</code>&#8216; function. It is called by SarahAjax object when there was error occured or the request reply is not normal.
<li>Initialize a SarahAjax object with Target URL to send request to.</li>
<li>Call the object&#8217;s &#8216;<code>sendRequest()</code>&#8216; method to send request to the URL</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #66cc66;">&lt;</span>html<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>head<span style="color: #66cc66;">&gt;&lt;</span>title<span style="color: #66cc66;">&gt;</span>SarahAjax Testing <span style="color: #CC0000;">1</span> using prototyping<span style="color: #66cc66;">&lt;/</span>title<span style="color: #66cc66;">&gt;</span>
&nbsp;
&nbsp;
<span style="color: #66cc66;">&lt;</span>script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span> src=<span style="color: #3366CC;">&quot;sarahajax.js&quot;</span><span style="color: #66cc66;">&gt;&lt;/</span>script<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//The action function must have the  argument for XMLHttpRequest object</span>
SarahAjax.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">action</span>= <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #009900; font-style: italic;">//obj.responseText : Regualar string</span>
	<span style="color: #009900; font-style: italic;">//obj.statusText : Showing Requst Result Status</span>
	<span style="color: #009900; font-style: italic;">//obj.responseXML : XML string</span>
	<span style="color: #003366; font-weight: bold;">var</span> div = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;result&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>div<span style="color: #66cc66;">&#41;</span>
		div.<span style="color: #006600;">innerHTML</span> = obj.<span style="color: #006600;">responseText</span>; 
	<span style="color: #000066; font-weight: bold;">else</span>
		<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;hey where should I show the reply text?&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//error functino is optional, but it's a good idea to create one.</span>
SarahAjax.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">error</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>obj.<span style="color: #006600;">statusText</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//Now create an object. The request will be sent to target_app.php</span>
<span style="color: #003366; font-weight: bold;">var</span> sajax = <span style="color: #003366; font-weight: bold;">new</span> SarahAjax<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;target_app.php&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #66cc66;">&lt;/</span>script<span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;</span>style type=<span style="color: #3366CC;">&quot;text/css&quot;</span><span style="color: #66cc66;">&gt;</span>
#result <span style="color: #66cc66;">&#123;</span>
	border:1px dashed black;
	width:300px;
	height:30px;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&lt;</span><span style="color: #0066FF;">/style&gt;&lt;/</span>head<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>body<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>h2<span style="color: #66cc66;">&gt;</span>This <span style="color: #000066; font-weight: bold;">is</span> SarahAjax Testing Page<span style="color: #66cc66;">&lt;/</span>h2<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>div id=<span style="color: #3366CC;">&quot;result&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #000066; font-weight: bold;">This</span> <span style="color: #000066; font-weight: bold;">is</span> where the result should show.
<span style="color: #66cc66;">&lt;/</span>div<span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;</span>a href=<span style="color: #3366CC;">'javascript:sajax.sendRequest();'</span><span style="color: #66cc66;">&gt;</span>Send Request<span style="color: #66cc66;">&lt;/</span>a<span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;/</span>body<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>html<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p><a href='/sarahajax/usage2.html'><strong>Click here for Usage Example2: inherit SarahAjax class</strong></a><br />
You can also use the class by creating a subclass. Please Click on the title to see the source in action. </p>
<p><a href='/sarahajax.zip'><strong>Download sarahajax.zip</strong></a></p>
]]></content:encoded>
			<wfw:commentRss>http://code.neox.net/2008/06/03/sarahajax-javascript-ajax-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
