<?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>FiddyP &#187; javascript</title>
	<atom:link href="http://fiddyp.co.uk/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://fiddyp.co.uk</link>
	<description>The personal blog of Andy Bailey</description>
	<lastBuildDate>Tue, 17 Jan 2012 09:02:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Bypass / Disable print dialog box in firefox with javascript print()</title>
		<link>http://fiddyp.co.uk/bypass-disable-print-dialog-box-in-firefox-with-javascript-print/</link>
		<comments>http://fiddyp.co.uk/bypass-disable-print-dialog-box-in-firefox-with-javascript-print/#comments</comments>
		<pubDate>Sat, 01 Jan 2011 18:23:43 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[dialog box]]></category>
		<category><![CDATA[disable]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[profit]]></category>

		<guid isPermaLink="false">http://fiddyp.co.uk/?p=1887</guid>
		<description><![CDATA[Just print! In my ePoS system project, I have the need to open a popup with javascript and print the contents without seeing the dialog box. Everywhere I [..]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ffiddyp.co.uk%2Fbypass-disable-print-dialog-box-in-firefox-with-javascript-print%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ffiddyp.co.uk%2Fbypass-disable-print-dialog-box-in-firefox-with-javascript-print%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img src="http://fiddyp.co.uk/wp-content/uploads/no-print-dialog.png" alt="" title="no-print-dialog" width="257" height="196" class="aligncenter size-full wp-image-1900" /></p>
<h2>Just print!</h2>
<p>In my ePoS system project, I have the need to open a popup with javascript and print the contents without seeing the dialog box.</p>
<p>Everywhere I searched, there were the &#8216;you don&#8217;t wanna do that because&#8230;.&#8217; from know it all keyboard warriors saying it was bad because evil doers could send porn to your printer. </p>
<h2>What about intranets and web apps?</h2>
<p>Finally, I figured it out and as long as you&#8217;re using Firefox and you have access to it then you&#8217;re ready to make it print without a nasty dialog&#8230;</p>
<ul>
<li>Open firefox and put <strong>about:config</strong> in the address bar and press enter (you will need to click the button that shows to say you know what you&#8217;re doing)</li>
<li>Type <strong>print.always_print_silent</strong> in the filter box at the top<br />
If you have an entry already there and it is set to false just right click it and choose &#8216;toggle&#8217; to set it to true.<br />
If there is no entry there already then follow on&#8230;</p>
<ul>
<li>right click the screen and choose New->boolean</li>
<li>enter <strong>print.always_print_silent</strong> as the preference name when asked and press enter</li>
<li>set boolean value to <strong>true</strong> and press enter</li>
</ul>
</li>
</ul>
<p>That&#8217;s it, you will no longer see the dialog box when you print. Beware though, it will try to print to the last printer you used when you had the dialog box open. If you ever need to change it then you will need to toggle the value for print.always_print_silent by following the above steps.</p>
<h2>Open a javascript popup and print the contents</h2>
<p>Now you can print without dialogs, you can use the following code to open a popup window and print it.</p>
<h3>open the popup</h3>
<p><code><br />
windowname = window.open('url.html','name',options);<br />
</code></p>
<p>the options you have available are:</p>
<table border="0" cellspacing="5" cellpadding="5">
<tbody>
<tr>
<th>Option</th>
<th>Values</th>
<th>Description</th>
</tr>
<tr>
<td>location</td>
<td>yes|no</td>
<td>Does the location bar show?</td>
</tr>
<tr>
<td>menubar</td>
<td>yes|no</td>
<td>Does the menubar show?</td>
</tr>
<tr>
<td>scrollbars</td>
<td>yes|no</td>
<td>Do scrollbars show?</td>
</tr>
<tr>
<td>status</td>
<td>yes|no</td>
<td>Does the status bar show|</td>
</tr>
<tr>
<td>titlebar</td>
<td>yes|no</td>
<td>Does the titlebar show?</td>
</tr>
<tr>
<td>toolbar</td>
<td>yes|no</td>
<td>Does the toolbar show?</td>
</tr>
<tr>
<td>resizable</td>
<td>yes|no</td>
<td>Can you resize the window?</td>
</tr>
<tr>
<td>height</td>
<td>pixels</td>
<td>height of window</td>
</tr>
<tr>
<td>width</td>
<td>pixels</td>
<td>width of window</td>
</tr>
<tr>
<td>directories</td>
<td>yes|no</td>
<td>Does the personal toolbar show?</td>
</tr>
<tr>
<td>innerHeight</td>
<td>pixels</td>
<td>specifies the inner height of window</td>
</tr>
<tr>
<td>innerWidth</td>
<td>pixels</td>
<td>specifies the inner width of window</td>
</tr>
<tr>
<td>screenX</td>
<td>pixels</td>
<td>specifies distance from left edge of screen</td>
</tr>
<tr>
<td>screenY</td>
<td>pixels</td>
<td>specifies distance from top edge of screen</td>
</tr>
</tbody>
</table>
<p>For example, to open a pop window showing the file called <strong>example.html</strong> with no menu bar, with a width of 430 pixels, height of 700 pixels, no toolbar, with scrollbars and with the window you would use this command</p>
<p><code><br />
winname = window.open('/example.html','newwin','menubar=no,width=430,height=700,toolbar=no,scrollbars=yes,screenX=300');<br />
</code></p>
<p>To close that window, you can add this</p>
<p><code><br />
winname.close();<br />
</code></p>
<p>To print the window, you can add this to the example.html page </p>
<p><code><br />
self.print();<br />
</code></p>
<p>or to print it from the calling page, you might need to pause for a little while before issuing the print command so it has time to load the page content first..</p>
<p><code><br />
setTimeout("winname.window.print();",500);<br />
</code></p>
<p>All the commands need to be within &lt;script&gt; /script tags</p>
]]></content:encoded>
			<wfw:commentRss>http://fiddyp.co.uk/bypass-disable-print-dialog-box-in-firefox-with-javascript-print/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Simple code to reveal asterisk (*&#039;s) password fields in your browser</title>
		<link>http://fiddyp.co.uk/simple-code-to-reveal-asterisk-s-password-fields-in-your-browser/</link>
		<comments>http://fiddyp.co.uk/simple-code-to-reveal-asterisk-s-password-fields-in-your-browser/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 16:36:32 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[asterisk]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[profit]]></category>
		<category><![CDATA[reveal]]></category>

		<guid isPermaLink="false">http://www.fiddyp.co.uk/?p=412</guid>
		<description><![CDATA[Here&#8217;s a little line of javascript that will work in Firefox to reveal the * characters in an auto populated password field. Just paste the following code into [..]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ffiddyp.co.uk%2Fsimple-code-to-reveal-asterisk-s-password-fields-in-your-browser%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ffiddyp.co.uk%2Fsimple-code-to-reveal-asterisk-s-password-fields-in-your-browser%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img alt="" src="http://www.fiddyp.co.uk/wp-content/uploads/2008/07/revealasteriskpassword.gif" class="alignnone" width="429" height="420" /></p>
<p>Here&#8217;s a little line of javascript that will work in Firefox to reveal the * characters in an auto populated password field. Just paste the following code into your browser address bar whenever you see an asterisked form field and press enter..</p>
<pre class="brush: plain; title: ; notranslate"> javascript:var els = document.getElementsByTagName('input'); for(var x=0; x&amp;lt;els.length; x++){ if(els[x].type.toLowerCase() == 'password' ){ var test = els[x].type = 'text';}} </pre>
<p>Useful if <del>if you want to access your sisters email account</del> you forget a password that gets auto filled.</p>
]]></content:encoded>
			<wfw:commentRss>http://fiddyp.co.uk/simple-code-to-reveal-asterisk-s-password-fields-in-your-browser/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>CommentLuv Ajax Preview, now platform independent !</title>
		<link>http://fiddyp.co.uk/commentluv-ajax-preview-now-platform-independent/</link>
		<comments>http://fiddyp.co.uk/commentluv-ajax-preview-now-platform-independent/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 22:22:38 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[Blog News]]></category>
		<category><![CDATA[Blog Tools]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Social Networking]]></category>
		<category><![CDATA[commentluv]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[profit]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.fiddyp.co.uk/?p=352</guid>
		<description><![CDATA[I spent a large part of last weekend coding some changes to CommentLuv, well, changing the way it can be called and how it displays posts and such. [..]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ffiddyp.co.uk%2Fcommentluv-ajax-preview-now-platform-independent%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ffiddyp.co.uk%2Fcommentluv-ajax-preview-now-platform-independent%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I spent a large part of last weekend coding some changes to CommentLuv, well, changing the way it can be called and how it displays posts and such. Here&#8217;s a little preview of what sort of thing it can do on a dummy form, let me know if it can&#8217;t pick up your last post or if you experience any issues with it&#8230;</p>
<p>This is running as just javascript and <a href="http://en.wikipedia.org/wiki/JQuery" title="JQuery" rel="wikipedia" target="_blank" class="zem_slink">jquery</a> without using any of the wordpress core functions and it should work on standard forms on sites that don&#8217;t have PHP enabled. I just need to tidy up the code and package it in an easy to install way and it&#8217;ll open up CommentLuv to many more platforms&#8230;yey!</p>
<p>I&#8217;m not 100% sure yet but this could mean a blogspot commentluv plugin, I&#8217;m trying some experiments now and so far the results are promising&#8230;
<div id="zemanta-pixie" style="margin: 5px 0pt;width: 100%"><a id="zemanta-pixie-a" href="http://www.zemanta.com/" title="Zemified by Zemanta"><img src="http://img.zemanta.com/pixie.png?x-id=e822c8d3-e8df-4c70-a9f1-64d4d0cbf747" style="border: medium none;float: right"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://fiddyp.co.uk/commentluv-ajax-preview-now-platform-independent/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>Beginners AJAX Tutorial Series Part 5</title>
		<link>http://fiddyp.co.uk/beginners-ajax-tutorial-series-part-5/</link>
		<comments>http://fiddyp.co.uk/beginners-ajax-tutorial-series-part-5/#comments</comments>
		<pubDate>Fri, 04 Jan 2008 11:17:11 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajaxseries]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[profit]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web 2.0]]></category>

		<guid isPermaLink="false">http://www.fiddyp.co.uk/beginners-ajax-tutorial-series-part-5/</guid>
		<description><![CDATA[This is part 5 of the AJAX Tutorial Series. You can see the other Beginners AJAX Tutorials here. Part 1 &#8211; An introduction to AJAX Part 2 &#8211; [..]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ffiddyp.co.uk%2Fbeginners-ajax-tutorial-series-part-5%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ffiddyp.co.uk%2Fbeginners-ajax-tutorial-series-part-5%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img src="http://fiddyp.co.uk/wp-content/uploads/2007/11/ajaxavenue.gif" alt="AJAX Avenue" /><br />
This is part 5 of the AJAX Tutorial Series.<br />
You can see the other Beginners AJAX Tutorials here.</p>
<ul>
<li><a href="http://www.fiddyp.co.uk/beginners-ajax-tutorial-series-part-1/">Part 1 &#8211; An introduction to AJAX</a></li>
<li><a href="http://www.fiddyp.co.uk/beginners-ajax-tutorial-part-2/">Part 2 &#8211; Hello world!</a></li>
<li><a href="http://www.fiddyp.co.uk/beginners-ajax-tutorial-series-part-3">Part 3 &#8211; The Object of my desire</a></li>
<li><a href="http://www.fiddyp.co.uk/beginners-ajax-tutorial-series-part-4">Part 4 &#8211; Are you ready?</a></li>
</ul>
<p>Hurrah! this tutorial will actually download some data (yey!) We will use everything we have learned so far and add just a little bit that will allow us to download data from the server and put it somewhere on the page without a refresh. (I know, you must have goosebumps!) <img src='http://fiddyp.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h3>XMLHttpRequestObject.send(null);</h3>
<p>We need this command to initiate the object send. We created an object, created a place for the data to go, we created a routine to wait patiently for the object to be ready and sent the object to the data source using the above command. The patient routine for the waiting will take the response to the data source and put it in the div we created in the HTML of the page.</p>
<p>We can replace the bit from the last bit of complete code (on part 4) with this command and instead of it saying it&#8217;s ready to download, it will actually do the fetching for the <strong>obj.innerHTML=XMLHttpRequestObject.responseText;</strong> which is called when the readyState and Status are ok.</p>
<p>[HTML]</p>
<p>	  var XMLHttpRequestObject = false;<br />
	  if (window.XMLHttpRequest) {<br />
		XMLHttpRequestObject = new XMLHttpRequest();<br />
	  } else if (window.ActiveXObject) {<br />
		XMLHttpRequestObject = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
	  }<br />
	  function getData(dataSource, divID)<br />
	  {<br />
		if(XMLHttpRequestObject) {<br />
		  var obj = document.getElementById(divID);<br />
		  XMLHttpRequestObject.open(&#8220;GET&#8221;, dataSource);<br />
		  XMLHttpRequestObject.onreadystatechange = function()<br />
		  {<br />
			if (XMLHttpRequestObject.readyState == 4 &amp;&amp;<br />
			  XMLHttpRequestObject.status == 200) {<br />
				obj.innerHTML=XMLHttpRequestObject.responseText;<br />
			}<br />
		  }<br />
	   XMLHttpRequestObject.send(null);<br />
		}<br />
	  }</p>
<h1>Fetching data with Ajax</h1>
<form></form>
<div id="targetDiv">
<p>The fetched data will go here.</p>
</p></div>
<p>[/HTML]</p>
<p>I&#8217;ll try and make this code easier to understand with a picture using pretty colours! <img src='http://fiddyp.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
<a href='http://fiddyp.co.uk/wp-content/uploads/2008/01/part5picture.gif' title='Part 5 AJAX picture'><img src='http://fiddyp.co.uk/wp-content/uploads/2008/01/part5picture-150x150.gif' alt='Part 5 AJAX picture' /></a></p>
<p>The blue bit gets called when the user clicks the button, if the object was successfully created then create a routine that &#8216;listens&#8217; to the objects readyState and status. Then the <strong>XMLHttpRequestObject.send(null);</strong> command does the action and when that goes ahead and activates <strong>XMLHttpRequestObject.open(&#8220;GET&#8221;, dataSource);</strong> which goes off and gets the data pointed to by dataSource which of course changes the objects status and readyState. When it is at what we want, we inject the responseText to the div pointed to by divID.</p>
<p>Simple! :-/</p>
<p>You can see it in action <a href="http://www.fiddyp.co.uk/internal/ajax_tutorials/part5/part5.html">here</a></p>
<p>Exciting huh? LOL . It&#8217;ll display anything that is in the oranges.txt file, not much use yet but at least we know we can fetch data!</p>
<p>What if, instead of displaying the contents of a text file, we used a php script to &#8216;echo&#8217; out something?.. replace oranges.txt with this code..</p>
<p>[PHP]<br />
echo &#8220;these are some oranges created by php!&#8221;;<br />
echo &#8220;<br />ooooooooooooooooooooooooooooooooo&#8221;;<br />
?&gt;<br />
[/PHP]</p>
<p>(don&#8217;t forget to enclose any php script with &lt;?php and ?&gt; or it wont work &#8211; I cant put them in here otherwise it will run the script instead of showing the code. Also, change the onclick event in the html to call oranges.php (if you saved the php with that name) instead of oranges.txt</p>
<p>You can see that in action <a href="http://www.fiddyp.co.uk/internal/ajax_tutorials/part5/part5-2.html">here</a></p>
<p>There may be a pause between you clicking and the message being displayed. We can let the user know that something is happening by adding another <strong>if</strong> condition to look for a readyState of 1 (loading) and displaying the message &#8220;off to get oranges&#8230;&#8221; like this..</p>
<pre class="brush: xml; title: ; notranslate">
if (XMLHttpRequestObject.readyState == 1)
{
	obj.innerHTML=&quot;off to get oranges...&quot;;
}</pre>
<p>we just chuck that into the anonymous function that is called when the readyState changes&#8230;</p>
<pre class="brush: xml; title: ; notranslate">
XMLHttpRequestObject.onreadystatechange = function()
          {
            if (XMLHttpRequestObject.readyState == 4 &amp;amp;&amp;amp;
              XMLHttpRequestObject.status == 200) {
		obj.innerHTML=XMLHttpRequestObject.responseText;

            }
	    if (XMLHttpRequestObject.readyState == 1)
	    {
	 	obj.innerHTML=&quot;off to get oranges...&quot;;
	    }
          }
</pre>
<p>See that in action <a href="http://www.fiddyp.co.uk/internal/ajax_tutorials/part5/part5-3.html">here</a></p>
<p>You can download all the files used in this tutorial <a href="http://www.fiddyp.co.uk/internal/ajax_tutorials/part5/part5.zip">here</a>.</p>
<p>Now we&#8217;re getting somewhere! But, what if we want to send data to the php file so it can do something more than print a hardcoded message?<br />
Actually, it&#8217;s quite easy! come back next week to see the final part where we&#8217;ll send something to the php file and have it send back something it made with what we sent. (it could be a username or contents of a text field &#8211; think form validation!)</p>
<p>You can subscribe to my feed with the button at the top right of this blog or add your email to the box on the sidebar of the main page to subscribe by email. I hope you can come back and learn a bit more, enough at least to make your own php script and AJAX page&#8230;.</p>
]]></content:encoded>
			<wfw:commentRss>http://fiddyp.co.uk/beginners-ajax-tutorial-series-part-5/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Beginners AJAX Tutorial Series Part 4</title>
		<link>http://fiddyp.co.uk/beginners-ajax-tutorial-series-part-4/</link>
		<comments>http://fiddyp.co.uk/beginners-ajax-tutorial-series-part-4/#comments</comments>
		<pubDate>Thu, 20 Dec 2007 09:58:24 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[ajaxseries]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[profit]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web 2.0]]></category>

		<guid isPermaLink="false">http://www.fiddyp.co.uk/beginners-ajax-tutorial-series-part-4/</guid>
		<description><![CDATA[This is part 4 of the AJAX Tutorial Series. You can see the other Beginners AJAX Tutorials here. Part 1 &#8211; An introduction to AJAX Part 2 &#8211; [..]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ffiddyp.co.uk%2Fbeginners-ajax-tutorial-series-part-4%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ffiddyp.co.uk%2Fbeginners-ajax-tutorial-series-part-4%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img src='http://fiddyp.co.uk/wp-content/uploads/2007/11/ajaxavenue.gif' alt='Ajax Avenue Street Sign' /><br />
This is part 4 of the AJAX Tutorial Series.<br />
You can see the other Beginners AJAX Tutorials here.</p>
<ul>
<li><a href="http://www.fiddyp.co.uk/beginners-ajax-tutorial-series-part-1/">Part 1 &#8211; An introduction to AJAX</a></li>
<li><a href="http://www.fiddyp.co.uk/beginners-ajax-tutorial-part-2/">Part 2 &#8211; Hello world!</a></li>
<li><a href="http://www.fiddyp.co.uk/beginners-ajax-tutorial-series-part-3">Part 3 &#8211; The Object of my desire</a></li>
</ul>
<p>In this part of the tutorial series we will get ready to <del>rumble!</del> download data using the object we created in the last part&#8230;</p>
<h3>onreadystatechange</h3>
<p>What the hell is that? lol, it&#8217;s not move states when you&#8217;re ready.. it&#8217;s what we use on the object we created to see what state it is in, the different states are:</p>
<ul>
<li>0 &#8211; Uninitialized</li>
<li>1 &#8211; Loading</li>
<li>2 &#8211; Loaded</li>
<li>3 &#8211; Interactive</li>
<li>4 &#8211; Complete</li>
</ul>
<p>We can check for an objects state to see where it&#8217;s at to decide if we can output the result or not, we can combine this with a test for the HTTP status of a download to see if it was successful, we can check for a value of 200 which means everything went ok. (a value of 404 would mean file not found just like normal HTTP status codes).</p>
<p>To use the readystate property, just bang it on the end of the object name and put that in an &#8216;if&#8217; statement. Ie&#8230;</p>
<p><code>if (XMLHttpRequestObject.readyState == 4 &amp;&amp; XMLHttpRequestObject.status == 200)<br />
{do something}</code></p>
<p>(the &amp;&amp; means AND, so we check the readystate AND the status, if they are BOTH correct then execute the code in the curly brackets)</p>
<p>To put that in the code of the last tutorial, replace the bit that outputs the message about the oranges box being collected with an anonymous function that is attached to the objects readystate. It&#8217;s not named an anonymous function because it has a secret identity, it is just a nice way of running a function with no name when a certain thing happens like the objects&#8217; readystate being changed.</p>
<p>For example, if we put<br />
<code>XMLHttpReqestObject.onreadystatechange = function()<br />
{<br />
do something<br />
}<br />
</code></p>
<p>We have attached the onreadystatechange to the object we created, as soon as that objects readystate has altered, the anonymous function takes action and executes the commands contained in the curly brackets ({do something }).</p>
<p>That&#8217;s exactly where we put the test to see what the HTTP status and readystate number is. Ie.</p>
<p><code>XMLHttpRequestObject.onreadystatechange = function()<br />
          {<br />
            if (XMLHttpRequestObject.readyState == 4 &amp;&amp; XMLHttpRequestObject.status == 200)<br />
             {<br />
              data downloaded, do something with it...<br />
             }<br />
          } </code></p>
<p>You may have noticed little graphics that are synonymous with web 2.0 and AJAX ( <img src='http://fiddyp.co.uk/wp-content/uploads/2007/12/ajaxloading.gif' alt='ajax loading' /> ). You can add one of those to let the user see something is happening. In the code above, you can check for a readyState of 1 (loading) and add some text like this:</p>
<p><code>XMLHttpRequestObject.onreadystatechange = function()<br />
          {<br />
            if (XMLHttpRequestObject.readyState == 1)<br />
             {<br />
              obj.innerHtml="Loading...";<br />
             }</p>
<p>            if (XMLHttpRequestObject.readyState == 4 &amp;&amp; XMLHttpRequestObject.status == 200)<br />
             {<br />
              data downloaded, do something with it...<br />
             }<br />
          } </code></p>
<p><em>The FiddyP amazing analogy:</em><br />
Last time we had created a box for the oranges that we want from the AJAX shop<br />
<code>XMLHttpRequestObject = new XMLHttpRequest();</code><br />
then we opened it and sent  it to the AJAX shop with a label of what we want.<br />
<code>XMLHttpRequestObject.open("GET", dataSource);</code><br />
And we wait for the signal that the box we sent was filled ok and ready to go<br />
<code>if (XMLHttpRequestObject.readyState == 4 &amp;&amp; XMLHttpRequestObject.status == 200)</code></p>
<p>Your whole code will now look like this&#8230;</p>
<pre class="brush: xml; title: ; notranslate">

    &lt;title&gt;FiddyP AJAX Tutorial Series Part 4&lt;/title&gt;

      var XMLHttpRequestObject = false;
      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
      }
      function getData(dataSource, divID)
      {
        if(XMLHttpRequestObject) {
          var obj = document.getElementById(divID);
          XMLHttpRequestObject.open(&quot;GET&quot;, dataSource);

          XMLHttpRequestObject.onreadystatechange = function()
          {
            if (XMLHttpRequestObject.readyState == 4 &amp;amp;&amp;amp;
              XMLHttpRequestObject.status == 200) {
            }
          }
	   obj.innerHTML = &quot;Ready to download&quot;;

        }
      }

    &lt;h1&gt;Fetching data with Ajax&lt;/h1&gt;
    &lt;form&gt;

    &lt;/form&gt;
    &lt;div id=&quot;targetDiv&quot;&gt;
      &lt;p&gt;The fetched data will go here.&lt;/p&gt;
    &lt;/div&gt;
</pre>
<p>OK, so you have your box at the AJAX shop full of oranges.txt, next tutorial we&#8217;ll do something with the oranges&#8230;</p>
<p>If you&#8217;ve been following the other tutorials then the code above shouldn&#8217;t be so hard to understand (shyeah right!), it&#8217;s just a bit more added to what we&#8217;ve already covered. You can copy the code from above and try it out yourself or you can click <a href="http://www.fiddyp.co.uk/internal/ajax_tutorials/part4/getReady.html">here</a> to see it in action (hold on to your hat <img src='http://fiddyp.co.uk/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' /> )</p>
<p>We&#8217;re still in the AJAX starting blocks waiting for the bang to start us on the AJAX runway but don&#8217;t despair, next tutorial we will actually download data from a server and display it on the page without a reload which is what AJAX is all about. Once we can actually display the data, we can start doing things with it and then we can start to make pretty little scripts that do amazing things like count the amount of times a pink box has been clicked. hehehe, we&#8217;ll do more than that I promise!</p>
]]></content:encoded>
			<wfw:commentRss>http://fiddyp.co.uk/beginners-ajax-tutorial-series-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beginners AJAX Tutorial Series Part 3</title>
		<link>http://fiddyp.co.uk/beginners-ajax-tutorial-series-part-3/</link>
		<comments>http://fiddyp.co.uk/beginners-ajax-tutorial-series-part-3/#comments</comments>
		<pubDate>Tue, 11 Dec 2007 15:37:24 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[ajaxseries]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[profit]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web 2.0]]></category>

		<guid isPermaLink="false">http://www.fiddyp.co.uk/beginners-ajax-tutorial-series-part-3/</guid>
		<description><![CDATA[This is part 3 of the AJAX Tutorial Series. You can see the other Beginners AJAX Tutorials here. Part 1 &#8211; An introduction to AJAX Part 2 &#8211; [..]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ffiddyp.co.uk%2Fbeginners-ajax-tutorial-series-part-3%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ffiddyp.co.uk%2Fbeginners-ajax-tutorial-series-part-3%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img src='http://fiddyp.co.uk/wp-content/uploads/2007/11/ajaxavenue.gif' alt='Ajax Avenue Street Sign' /><br />
This is part 3 of the AJAX Tutorial Series.<br />
You can see the other Beginners AJAX Tutorials here.</p>
<ul>
<li><a href="http://www.fiddyp.co.uk/beginners-ajax-tutorial-series-part-1/">Part 1 &#8211; An introduction to AJAX</a></li>
<li><a href="http://www.fiddyp.co.uk/beginners-ajax-tutorial-part-2/">Part 2 &#8211; Hello world!</a></li>
</ul>
<p>In this part of the tutorial series we will open the object that you created in the last tutorial. So far, we have created an object to handle the data and made sure it is created in whatever browser you are using. Now, if we want to work with the created object, we need to open it&#8230;</p>
<h3>Open The XMLHttpRequest Object</h3>
<p>Opening the object prepares it for use to fetch the data from a server, the act of opening the object doesn&#8217;t actually fetch any data yet.</p>
<p><em>The FiddyP amazing analogy</em> :- Imagine you created a page to collect a box of oranges from the local AJAX fruit shop. In the last tutorial, you created the box (object), now you need to open it so the AJAX oranges can fit inside&#8230;</p>
<p>The open comand: [square brackets] = optional arguments.</p>
<p><code>open("method", "url" [, asyncFlag[, "username"[, "password"]]])</code></p>
<p>Explanation:</p>
<ul>
<li><strong>method</strong> is the HTTP method to use when opening the connection. It can be &#8216;GET&#8217;, &#8216;POST&#8217;, &#8216;PUT&#8217; or &#8216;HEAD&#8217;</li>
<li><strong>url</strong> is the URL you want to download from</li>
<li><strong>asyncFlag</strong> is a Boolean (one or the other) value used to indicate that the call is asynchronous. (no waiting for it to finish)<br />
This defaults to TRUE or 1 so is not needed unless you want to specify that the page waits for completion.</li>
<li><strong>username</strong> is if you need a username to access the page identified by URL</li>
<li><strong>password</strong> is for a password if it&#8217;s required.</li>
</ul>
<p>Make sense? lol, maybe not. Here&#8217;s how it looks in use:</p>
<p><code>XMLHttpRequestObject.open("GET","oranges.txt");</code></p>
<p>We just add the <em>open</em> bit of code on to the end of the name of the object we created. Here&#8217;s an example page where we create a form in HTML and add a button for the user to click to open a remote file on the server with AJAX. Because AJAX uses CSS to change elements of the page, we need to create a &lt;div&gt; on the page to show the data we retrieve from the remote file. We&#8217;ll also have to create a function to take action when the button is clicked (we can&#8217;t do it the normal way with the &lt;form&gt; action because that would cause the page to reload).</p>
<p>Here&#8217;s the form and div html:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;form&gt;

&lt;/form&gt;

&lt;div id=&quot;targetDiv&quot;&gt;
  &lt;p&gt;The oranges box will go here.&lt;/p&gt;
&lt;/div&gt; </pre>
<p>You can see that the button has an &#8216;onclick&#8217; event. That&#8217;s the javascript bit that calls the function when the button is clicked. The &lt;div&gt; has an id so the function will know where to put the collected data.</p>
<p>The javascript function is held in the &lt;head&gt; part of the document;</p>
<pre class="brush: jscript; title: ; notranslate">function getData(dataSource, divID)
      {
        if(XMLHttpRequestObject) {
          var obj = document.getElementById(divID);
          XMLHttpRequestObject.open(&quot;GET&quot;, dataSource);

          obj.innerHTML=&quot;Oranges Box Opened!&quot;;
        }
      }</pre>
<p>The function is called with the name of the data file we want (dataSource) and where to put the output (divID)<br />
The first line checks if the XMLHttpRequestObject was created (we did that last time so you know it should get to this point)<br />
If it finds that the object was created, it moves on to set the variable <strong>obj</strong> to the &lt;div&gt; we created by using <em>getElementByID</em><br />
Next, we use the open command on the object we created earlier, set the option to GET and point it to the external data file we specified when calling the function.<br />
Once that is done, we can output a message to the &lt;div&gt; we assigned to the variable <strong>obj</strong> by using <em>.innerHTML=</em></p>
<p>We&#8217;ll just put this function in place of the &#8220;hello world&#8221; message that we put in the last tutorial..</p>
<p>The whole code looks like this&#8230;</p>
<pre class="brush: xml; title: ; notranslate">

    &lt;title&gt;FiddyP AJAX tutorial, open the object&lt;/title&gt;

      var XMLHttpRequestObject = false;
      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
      }

      function getData(dataSource, divID)
      {
        if(XMLHttpRequestObject) {
          var obj = document.getElementById(divID);
          XMLHttpRequestObject.open(&quot;GET&quot;, dataSource);
          obj.innerHTML=&quot;Oranges Box Opened!&quot;;
        }
      }

    &lt;h1&gt;Fetching data with Ajax&lt;/h1&gt;
    &lt;form&gt;

    &lt;/form&gt;
    &lt;div id=&quot;targetDiv&quot;&gt;
      &lt;p&gt;The Oranges box will go here.&lt;/p&gt;
    &lt;/div&gt;
</pre>
<p>You can copy the contents of the HTML shown above and save it as a HTML file, you can also create a file called <strong>oranges.txt</strong> and put it in the same directory as the HTML file. (it doesn&#8217;t matter what is in the text file as we wont be doing anything with it other than to open it). Open the HTML file in your browser and try it yourself or click <a href="http://www.fiddyp.co.uk/internal/ajax_tutorials/part3/oranges.htm">here</a> to see it in action if you can&#8217;t wait.</p>
<p>That&#8217;s it for this week, we&#8217;re still not doing anything major yet but, be happy! we have so far, created an object and opened it. YEY! come back for part 4 where we will prepare to download information from the file we opened. (oh, I bet you can&#8217;t wait!!)</p>
<p>beep beep w00t!</p>
]]></content:encoded>
			<wfw:commentRss>http://fiddyp.co.uk/beginners-ajax-tutorial-series-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beginners AJAX Tutorial Part 2</title>
		<link>http://fiddyp.co.uk/beginners-ajax-tutorial-part-2/</link>
		<comments>http://fiddyp.co.uk/beginners-ajax-tutorial-part-2/#comments</comments>
		<pubDate>Mon, 03 Dec 2007 12:09:16 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[ajaxseries]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[profit]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web 2.0]]></category>

		<guid isPermaLink="false">http://www.fiddyp.co.uk/beginners-ajax-tutorial-part-2/</guid>
		<description><![CDATA[This is part 2 of the AJAX Tutorial Series. You can see the Beginners AJAX Tutorial Part 1 here. In this part, we will start you off with [..]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ffiddyp.co.uk%2Fbeginners-ajax-tutorial-part-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ffiddyp.co.uk%2Fbeginners-ajax-tutorial-part-2%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img src='http://fiddyp.co.uk/wp-content/uploads/2007/11/ajaxavenue.gif' alt='Ajax Avenue Street Sign' /><br />
<em>This is part 2 of the AJAX Tutorial Series. You can see the <a href="http://www.fiddyp.co.uk/beginners-ajax-tutorial-series-part-1/">Beginners AJAX Tutorial Part 1 here</a>.</em></p>
<p>In this part, we will start you off with some very simple code just to show you how things get started. At the end of this tutorial you will have made your own &#8216;hello world&#8217; AJAX script&#8230; (not a big deal I know, but you have to start somewhere!). I&#8217;ll keep these tutorials bitesize and introduce a new command or concept on each tutorial to add to what has already been covered. Today, we start at the beginning with something that will be in all future scripts&#8230;</p>
<h3>The XMLHttpRequest Object</h3>
<p>The what? <img src='http://fiddyp.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
The XMLHttpRequest object is at the heart of AJAX. It allows you to send and receive data with javascript without refreshing the page and works on all browsers (except, you guessed it! Internet Explorer). All you have to do is find out if the browser supports it with an &#8216;if&#8217; statement in the javascript contained in the &lt;head&gt; part of the html page and then create a XMLHttpRequest object using the &#8216;new&#8217; command;</p>
<pre class="brush: jscript; title: ; notranslate">if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();}</pre>
<p>That would create a XMLHttpRequest object and store it in XMLHttpRequestObject.</p>
<p>But, what about Internet Explorer?.. We can&#8217;t use XMLHttpRequest so we have to use an ActiveXObject. For us to do anything with an XMLHttpRequest  and make sure it works on both IE and Firefox, we can add this to the javascript so we now have..</p>
<pre class="brush: jscript; title: ; notranslate">if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
      }</pre>
<p>OK, that should cover us for making a XMLHttpRequest object, we can&#8217;t do anything in this tutorial series without it so to be sure, lets just create the whole html document and test to see if it can make an object by adding another &#8216;if&#8217; statement to output some text to the screen if the object creation was successful using the javascript command &#8216;document.write()&#8217;.</p>
<pre class="brush: xml; title: ; notranslate">

    &lt;title&gt;FiddyP Ajax Tutorial Part 2 - Test object creation&lt;/title&gt;

      var XMLHttpRequestObject = false;

      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
      }

      if (XMLHttpRequestObject) {
        document.write(&quot;&lt;h1&gt;Hello World! I Created an Object!&lt;/h1&gt;&quot;);
      }
</pre>
<p>You can click on the title of the code snippet above and see the plain text version, just copy this to your own file and save it as an HTML document or right click <a href="http://www.fiddyp.co.uk/internal/ajax_tutorials/FiddyP_AJAX_part2/createObject.html">here </a>and save it and test it. If it&#8217;s working, it will display the message &#8220;hello world! I created an object!&#8221; in big letters.</p>
<p>YEY! we created a &#8216;hello world&#8217; AJAX program!, I know it&#8217;s not much use yet but this is the foundation for all future tutorials so even thought it&#8217;s not that &#8216;sexy&#8217; , it IS important to make sure we can create the object that will handle the data being sent to and received from a remote file.</p>
<p>We can get into the sexy ins and outs of using this on the next tutorial. Please come back again to see how we can use this to open a remote file and display it&#8217;s contents on a HTML page without a reload&#8230;(ooh, I can&#8217;t wait&#8230;.)<br />
 <img src='http://fiddyp.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://fiddyp.co.uk/beginners-ajax-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Animated Sliding Div &#8211; easy peasy</title>
		<link>http://fiddyp.co.uk/animated-sliding-div-easy-peasy/</link>
		<comments>http://fiddyp.co.uk/animated-sliding-div-easy-peasy/#comments</comments>
		<pubDate>Mon, 10 Sep 2007 09:15:07 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[Blog Tools]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[animated]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[profit]]></category>
		<category><![CDATA[sliding]]></category>

		<guid isPermaLink="false">http://www.fiddyp.co.uk/?p=721</guid>
		<description><![CDATA[please see &#8211; http://www.fiddyp.co.uk/animated-div-slide]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ffiddyp.co.uk%2Fanimated-sliding-div-easy-peasy%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ffiddyp.co.uk%2Fanimated-sliding-div-easy-peasy%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>please see &#8211; <a href="http://www.fiddyp.co.uk/animated-div-slide">http://www.fiddyp.co.uk/animated-div-slide</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fiddyp.co.uk/animated-sliding-div-easy-peasy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

