<?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>Xylot Design &#187; Xylot Design</title>
	<atom:link href="http://xylotdesign.wordpress.com/author/xylotdesign/feed/" rel="self" type="application/rss+xml" />
	<link>http://xylotdesign.wordpress.com</link>
	<description>useful scripts, ideas, and tips!</description>
	<lastBuildDate>Fri, 21 Mar 2008 20:39:45 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='xylotdesign.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/d47e221117c84d128bbe00e39a6140a7?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Xylot Design &#187; Xylot Design</title>
		<link>http://xylotdesign.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://xylotdesign.wordpress.com/osd.xml" title="Xylot Design" />
		<item>
		<title>ASP: Reading and Writing Cookies</title>
		<link>http://xylotdesign.wordpress.com/2008/03/21/asp-reading-and-writing-cookies/</link>
		<comments>http://xylotdesign.wordpress.com/2008/03/21/asp-reading-and-writing-cookies/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 17:04:04 +0000</pubDate>
		<dc:creator>Xylot Design</dc:creator>
				<category><![CDATA[ASP]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[favorites]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[preferences]]></category>
		<category><![CDATA[sessions]]></category>
		<category><![CDATA[stats]]></category>
		<category><![CDATA[variables]]></category>
		<category><![CDATA[visitors]]></category>

		<guid isPermaLink="false">http://xylotdesign.wordpress.com/?p=11</guid>
		<description><![CDATA[Cookies are powerful things. Not only are they sweet and tasty, but they store useful information on your visitors&#8217; computers as well! You can use them to check if a user has visited your site before, or store their login information, preferences, favorites, and other stats.
But how do you use them in ASP? This tutorial [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=11&subd=xylotdesign&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Cookies are powerful things. Not only are they sweet and tasty, but they store useful information on your visitors&#8217; computers as well! You can use them to check if a user has visited your site before, or store their login information, preferences, favorites, and other stats.</p>
<p>But how do you use them in ASP? This tutorial shows you just that.<span id="more-11"></span></p>
<h1>The Commands</h1>
<p>To write a cookie to the user&#8217;s computer, we use the Response.Cookies command.</p>
<pre>Response.Cookies("CookieName") = SomeValue</pre>
<p>To retrieve a cookie&#8217;s value, we simply call the command.</p>
<pre>Dim CookieValue = Response.Cookies("CookieName")</pre>
<p>ASP has probably one of the easiest implementations of cookies of any programming language. There are other attributes of a cookie we can set as well.</p>
<p>Let&#8217;s say our user has logged in.</p>
<pre>&lt;%
    Dim strUserName = "Bob123"
    Response.Cookies("UserName") = strUserName
    Response.Cookies("IsLoggedIn") = True
    Response.Cookies("UserName").Expires = Now() + 60
    Response.Cookies("IsLoggedIn").Expires = Now() + 60
%&gt;</pre>
<p>We just gave the &#8220;UserName&#8221; and &#8220;IsLoggedIn&#8221; cookies an expiration date 60 days in the future (<b>Now() + 60</b>).</p>
<p>What if we want to check if our user is logged in, and display a welcome message accordingly?</p>
<pre>&lt;%
    If Response.Cookies("IsLoggedIn") = True Then
        Response.Write("Welcome, " &amp; Response.Cookies("UserName"))
    Else
        Response.Write("Welcome, stranger!")
    End If
%&gt;</pre>
<p>It&#8217;s as simple as that. But wait, there&#8217;s more! You can also make a cookie expire as soon as the user closes his or her browser; don&#8217;t set the Expires attribute at all.</p>
<p>To assign a specific date value for the expiration date:</p>
<pre>&lt;% Response.Cookies("UserName").Expires = #April 15, 2010# %&gt;</pre>
<h1>Cookie Arrays</h1>
<p>A cookie can contain a collection of multiple values. This is useful for, say, shopping carts where each item in your cart has several values assigned to it: product ID, quantity, base price, etc.</p>
<pre>&lt;%
    Response.Cookies("Item")("ItemID") = 1234
    Response.Cookies("Item")("BasePrice") = 74.95
    Response.Cookies("Item")("Name") = "Deluxe 4-Slice Toaster"
%&gt;</pre>
<p>Finally, what if you want to get rid of a cookie? Since cookies can&#8217;t be removed, per se, you can set them to expire <i>now</i> and they will essentially cease to exist.</p>
<pre>&lt;% Response.Cookies("UserName").Expires = Now() %&gt;</pre>
<p>As you can see, there are many uses for cookies, and in ASP it is really quite simple to utilize them. I never made any use of cookies for a long time; I stuck to session variables for everything. Once I actually got into cookies, I could never go back. They&#8217;re an invaluable resource.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/xylotdesign.wordpress.com/11/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/xylotdesign.wordpress.com/11/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/xylotdesign.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/xylotdesign.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/xylotdesign.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/xylotdesign.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/xylotdesign.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/xylotdesign.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/xylotdesign.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/xylotdesign.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/xylotdesign.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/xylotdesign.wordpress.com/11/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=11&subd=xylotdesign&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://xylotdesign.wordpress.com/2008/03/21/asp-reading-and-writing-cookies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b31bda9bb80f4c9b9f507a02539c79e7?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Xylot Design</media:title>
		</media:content>
	</item>
		<item>
		<title>JS: 10 (or so) Functions I Can&#8217;t Live Without</title>
		<link>http://xylotdesign.wordpress.com/2008/03/19/js-10-or-so-functions-i-cant-live-without/</link>
		<comments>http://xylotdesign.wordpress.com/2008/03/19/js-10-or-so-functions-i-cant-live-without/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 01:55:13 +0000</pubDate>
		<dc:creator>Xylot Design</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[capture]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[dollar]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[elements]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[handler]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[onload]]></category>
		<category><![CDATA[parent]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://xylotdesign.wordpress.com/?p=10</guid>
		<description><![CDATA[Below are ten JavaScript functions that, over the course of several years, I have found to be indispensable, awesome, and at times life-saving.
1. addEvent()
This function takes all the work out of determining the correct way to add an event to an element (depending on the user&#8217;s browser).
function addEvent(obj, eventtpye, functn, usecapture) {
    [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=10&subd=xylotdesign&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Below are ten JavaScript functions that, over the course of several years, I have found to be indispensable, awesome, and at times life-saving.<span id="more-10"></span></p>
<h1>1. addEvent()</h1>
<p>This function takes all the work out of determining the correct way to add an event to an element (depending on the user&#8217;s browser).</p>
<pre>function addEvent(obj, eventtpye, functn, usecapture) {
    if (obj.addEventListener) {
        obj.addEventListener(eventtype, functn, usecapture);
        return true;
    } else if (obj.attachEvent) {
       return obj.attachEvent('on' + eventtype, functn);
    } else {
        obj['on' + eventtype] = functn;
    }
}</pre>
<h1>2. addLoadEvent()</h1>
<p>This function adds events that trigger after the page has loaded by attaching them to the <b>onload</b> event handler. It first stores the existing <b>onload</b> contents to a variable. Then, if the <b>onload</b> handler is not set to a function, it assigned the given function to the handler. Otherwise, it adds the function to the existing <b>onload</b> contents.</p>
<pre>function addLoadEvent(functn) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = functn;
    } else {
        window.onload = function() {
            oldonload();
            functn();
        }
    }
}</pre>
<p><u><b>Note:</b></u> Another way of adding events to the <b>onload</b> handler is to simply use the <b>addEvent()</b> function, as described above, like so:</p>
<pre>addEvent(window, 'load', functn1, false);
addEvent(window, 'load', functn2, false);
// etc.</pre>
<h1>3. getElementsByClass()</h1>
<p>You would think that JavaScript would already have such a function, but surprisingly (or not, depending on your cynicism) it isn&#8217;t an original DOM method. We have <b>getElementById()</b>, <b>getElementsByName()</b>, and <b>getElementByTagName()</b>&#8230; so why not a <b>getElementsByClass()</b> method? Well, here it is:</p>
<pre>function getElementsByClass(findclass, node, tag) {
    var classElems = new Array();
    if (node == null)
        node = document;
    if (tag == null)
        tag = '*';
    var elems = node.getElementsByTagName(tag);
    var pattern = new RegExp('(^|\\\\s)'+finclass+'(\\\\s|$)');
    var j = 0;
    for (i = 0; i &lt; elems.length; i++) {
        if (pattern.test(elems[i].className)) {
            classElems[j] = elems[i];
            j++;
        }
    }
    return classElems;
}</pre>
<p>To return an array of all anchor (<b>&lt;a&gt;</b>) elements of the &#8220;foo&#8221; class, call the following:</p>
<pre>var foos = getElementsByClassName('foo', document, 'a');</pre>
<h1>4. toggle()</h1>
<p>The strict definition of &#8220;toggling&#8221; is hiding or showing an element upon the occurence of an event. This function, which does just that, is short, simple, and to the point.</p>
<pre>function toggle(objid) {
    var elem = document.getElementById(objid);
    elem.style['display'] = (elem.style['display'] == 'none') ? '' : 'none';
}</pre>
<h1>5. insertAfter()</h1>
<p>One would think that this would be a DOM core method (like <b>getElementsByClassName()</b>) but, sadly, it is not. Fortunately, it only requires one line of code, though it is hard to remember and is thus suited to having its own function.</p>
<pre>function insertAfter(parent, node, refNode) {
    if (refNode.nextSibling)
        parent.insertBefore(node, refNode.nextSibling);
    else
        parent.appendChild(node);
}</pre>
<h1>6. inArray()</h1>
<p>This is yet another simple method that really should already be built into the JavaScript DOM. While not a function, it is a prototype that extends the existing <b>Array</b> object to add this functionality.</p>
<pre>Array.prototype.inArray = function(value) {
    for (i = 0; i &lt; this.length; i++) {
        if (this[i] === value) return true;
    }
    return false;
}</pre>
<h1>7. getCookie(), setCookie(), delCookie()</h1>
<p>I included all three of these methods into one entry because they all go together like gears in a machine. PHP&#8217;s implementation of cookies is such an easy, painless process, but this is not so for JavaScript. These three will be like Vicodin after the pain of dealing with cookies in JS.</p>
<pre>function getCookie(name) {
    var start = document.cookie.indexOf(name + '=');
    var len = start + name.length + 1;
    if (!start &amp;&amp; name != document.cookie.substring(0, name.length)) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf( ';', len );
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}</pre>
<pre>function setCookie(name, value, expires, path, domain, secure) {
    var today = new Date(today.getTime());
    if (expires)
        expires = expires * 1000 * 60 * 60 * 24;
    var expires_date = new Date(today.getTime() + expires);
    document.cookie = name + '=' + escape(value) +
        (expires ? ';expires=' + expires_date.toGMTString() : '') +
        (path ? ';path=' + path : '') +
        (domain ? ';domain=' + domain : '') +
        (secure ? ';secure' : '' );
}</pre>
<pre>function delCookie(name, path, domain) {
    if (getCookie(name))
        document.cookie = name + '=' +
        (path ? ';path=' + path : '') +
        (domain ? ';domain=' + domain : '') +
        ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}</pre>
<h1>8. $()</h1>
<h2>The Prototype Dollar Function</h2>
<p>In all honesty, this function was new to me as of recently. It never really occurred to me to have a function whose name consists of one character (a dollar sign, no less!), that takes in either strings or objects and any number of arguments, and that returns either one object or an array of objects.</p>
<p>Now I can&#8217;t live without it. Here it is:</p>
<pre>function $() {
    var elems = new Array();
    for (var i = 0; i &lt; arguments.length; i++) {
        // For each argument passed to $()
        var elem = arguments[i];
        if (typeof elem == 'string')
            elem = document.getElementById(elem);
        if (arguments.length == 1)
            return elem;
        elems.push(elem);
    }
    return elems;
}</pre>
<p>It&#8217;s only a few lines of code, too! You can use it to call up one object in one way:</p>
<pre>var textbox = $('mytextbox');</pre>
<p>or many objects in many ways:</p>
<pre>var images = $(objImage1, 'myimg', 'badimage', objImagePlaceHolder);</pre>
<p>The return value is always an object, or an array of objects.</p>
<h1>9. getXMLHttpObject()</h1>
<p>If you&#8217;re working with AJAX and have to keep checking your notes for the function that returns the XML-HTTP object, this one&#8217;s for you. This works for nearly all browsers in existence.</p>
<pre>function getXMLHttpObject() {
    var xmlHttp;
    // IE only...
    try {
        xmlHttp = new ActiveXObject("Msxml2.xmlHttp");
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.xmlHttp");
        } catch (E) {
            xmlHttp = false;
        }
    }
    // All other browsers...
    if (!xmlHttp &amp;&amp; typeof xmlHttpRequest != 'undefined') {
        try {
            xmlHttp = new xmlHttpRequest();
        } catch (e) {
            xmlHttp = false;
        }
    }
    return xmlHttp;
}</pre>
<h1>10. randomRange()</h1>
<p>Last, but certainly not least, is the ultra-useful <b>randomRange()</b> function, which returns a random integer between two numbers, inclusive.</p>
<pre>function randomRange(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}</pre>
<h1>Conclusion</h1>
<p>I keep every single one of these functions inside one external JavaScript file called <b>common.js</b> and call it in the header file that I include in all pages of whatever site I&#8217;m working on. That way, all the code is available to me no matter what, whenever I need it. And trust me, I&#8217;ve needed it <i>plenty</i> of times in the past, and will continue to need them well into the future.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/xylotdesign.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/xylotdesign.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/xylotdesign.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/xylotdesign.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/xylotdesign.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/xylotdesign.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/xylotdesign.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/xylotdesign.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/xylotdesign.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/xylotdesign.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/xylotdesign.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/xylotdesign.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=10&subd=xylotdesign&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://xylotdesign.wordpress.com/2008/03/19/js-10-or-so-functions-i-cant-live-without/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b31bda9bb80f4c9b9f507a02539c79e7?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Xylot Design</media:title>
		</media:content>
	</item>
		<item>
		<title>JS: WYSISYG Rich Text Box</title>
		<link>http://xylotdesign.wordpress.com/2008/03/17/js-wysisyg-rich-text-box/</link>
		<comments>http://xylotdesign.wordpress.com/2008/03/17/js-wysisyg-rich-text-box/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 01:25:57 +0000</pubDate>
		<dc:creator>Xylot Design</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[commands]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[font]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[iframe]]></category>
		<category><![CDATA[rich text]]></category>
		<category><![CDATA[size]]></category>
		<category><![CDATA[textarea]]></category>
		<category><![CDATA[word processing]]></category>
		<category><![CDATA[wysiwyg]]></category>

		<guid isPermaLink="false">http://xylotdesign.wordpress.com/?p=9</guid>
		<description><![CDATA[Many sites are starting to add rich text boxes, which are text fields that let you create text and alter formatting in a WYSIWYG environment: &#8220;What You See Is What You Get&#8221;. This basically means a text box that is like a word processor; if you select text and click on the &#8220;Bold&#8221; button (such [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=9&subd=xylotdesign&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Many sites are starting to add rich text boxes, which are text fields that let you create text and alter formatting in a WYSIWYG environment: &#8220;What You See Is What You Get&#8221;. This basically means a text box that is like a word processor; if you select text and click on the &#8220;Bold&#8221; button (such as a bold letter <b>B</b>), the text will actually show up in bold.</p>
<p>If such a concept has eluded you, look no further for a detailed tutorial on how to create one.<span id="more-9"></span></p>
<p>If you have been thinking of a rich text field as its own control (like the <b>&lt;textarea&gt;</b> element), then you are thinking about it all wrong. Instead, think of the text field as being its own web page.  It makes sense, doesn&#8217;t it? You&#8217;re applying HTML formatting to the text, after all.</p>
<h1>The Structure</h1>
<p>The text field itself is an <b>&lt;iframe&gt;</b> element, which (quite conveniently) comes with a JavaScript function called <b>execCommand()</b>. This function will become your colleague and your confidant. Your most trusted ally, and your most intimate friend.</p>
<p>To start off, create a new page with an iframe, a regular button, a submit button, and a hidden text field. The regular button will toggle the <b>bold</b> setting of any selected text within the iframe. The submit button is there for decoration only, and as a reminder that sometimes a form will need to be submitted. As such, the iframe cannot be used as a form input field; instead, we will use the hidden text field to store the HTML code of the iframe element. Its contents will be updated upon submitting the form, as we will see later.</p>
<pre>&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Rich Text Editor&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form method="post" name="myform"&gt;
	&lt;input type="button" onclick="formatText('bold')" value="Bold" /&gt;&lt;br/&gt;
	&lt;iframe id="textbox" style="width: 300px; height: 150px"&gt;&lt;/iframe&gt;&lt;br/&gt;
	&lt;input type="submit" value="Submit" /&gt;
	&lt;input type="hidden" id="text" name="text" /&gt;
&lt;/form&gt;
&lt;/body&gt;
/html&gt;</pre>
<h1>The Backbone</h1>
<p>The <b>formatText()</b> function, called by clicking the &#8220;Bold&#8221; button, is defined below. You can either save it in an external <b>.js</b> file and call it from the HTML page, or add it to the <b>&lt;head&gt;</b> section.</p>
<pre>var objEditor;	// Globally define the text box object
unction formatText(action) {
	objEditor.execCommand(action, false, null);
}
window.onload = function() {
	objEditor = document.getElementById('textbox').contentWindow.document;
	objEditor.designMode = 'on';
	document.myform.onsubmit = function() {
		document.getElementById('text').value = objEditor.body.innerHTML;
	}
}</pre>
<p>As you can see above, the text editor object (with the ID &#8220;textbox&#8221;) is set to a global variable, which can then be called by any other functions. The <b>formatText()</b> function calls the omnipotent <b>execCommand()</b> function, which applies only to the <b>&lt;iframe&gt;</b> object and is extremely powerful. There is a complete list of commands below. The command used above is the &#8220;bold&#8221; command. The second argument determines whether a user interface is displayed. Since this is not needed, it is set to false. The third argument is the value of the command. Since we are toggling the &#8220;bold&#8221; setting, we set this to <b>null</b>.</p>
<p>Next, we define what will happen when the page loads by setting the document&#8217;s <b>onload</b> event handler to do a few things. First, the text editor object variable is set to the object itself. Next, the text editor iframe&#8217;s <b>designMode</b> setting is turned on; this makes it so that the HTML within the iframe is editable. This is a huge deal, because this is essentially what turns the iframe into a rich text field.</p>
<p>(Actually, you can even set the entire document&#8217;s <b>designMode</b> variable to &#8220;on&#8221;, rendering the entire document editable. Just imagine the possibilities!)</p>
<p>Finally, we set it so that when the user submits the form, it sets the &#8220;text&#8221; hidden field&#8217;s value to the HTML of the iframe.</p>
<h1>The Commands</h1>
<p>The following commands are some of the simpler commands that can be used: <b>Bold</b> (&lt;b&gt;&lt;/b&gt;), <b>Italic</b> (&lt;i&gt;&lt;/i&gt;), <b>Underline</b> (&lt;u&gt;&lt;/u&gt;), <b>StrikeThrough </b>(&lt;s&gt;&lt;/s&gt;), <b>SuperScript </b>(&lt;sup&gt;&lt;/sup&gt;), <b>SubScript </b>(&lt;sub&gt;&lt;/sub&gt;), <b>Cut</b>, <b>Copy</b>, <b>Paste</b>, <b>Undo</b>, <b>Redo</b>, <b>InsertOrderedList</b> (&lt;ol&gt;&lt;/ol&gt;), <b>InsertUnorderedList</b> (&lt;ul&gt;&lt;/ul&gt;), <b>Outdent</b>, <b>Indent</b>, <b>JustifyLeft</b>, <b>JustifyCenter</b>, <b>JustifyRight</b>, and <b>JustifyFull</b>.</p>
<p>SuperScript and SubScript raise or lower text, for things such as mathematic variables (like 7<sup>2</sup> and H<sub>2</sub>O). The commands Cut, Copy, Paste, Undo and Redo function the same way they do in word processors. InsertOrderedList inserts a numbered (ordered) list, while InsertUnorderedList inserts a bulleted (unordered) list. Outdent moves a paragraph of text to the left, while Indent moves it inward (to the right). The Justify commands align the text to the direction specified.</p>
<h2>Change Font Size</h2>
<p>The <b>FontSize</b> command changes the size of the selected text in the iframe, and requires a value from 1 to 7, 1 being the smallest size, 7 being quite large. This command is based on the soon-to-be obsolete <b>&lt;font&gt;</b> tag, which uses the same values for its <b>size</b> attribute.</p>
<p>This function is best used with a drop-down list whose <b>onchange</b> event handler calls a custom function that changes the font size to the selected value. To most accurately show the user what the end result would be, each line should resemble the following.</p>
<pre>&lt;option value="X"&gt;&lt;font size="X"&gt;X&lt;/font&gt;&lt;/option&gt;</pre>
<p>Each item&#8217;s value is the size number, while the item is shown in that size, displaying the size number.</p>
<h1>Change Font Name</h1>
<p>There is a certain list of fonts that is safe to assume all users have installed on their systems. Known as the <b>Core Fonts</b>, these include Andale Mono, Arial, Comic Sans MS, Courier New, Georgia, Impact, Times New Roman, Trebuchet MS, Verdana, and Webdings. It is also safe to include a few fonts that are on most users&#8217; systems, but not all: Arial Black, Lucida Console, Lucida Sans Unicode, Tahoma, and Wingdings.</p>
<p>Like we did with the font sizes, if we had a drop-down list of fonts, each displayed in its own font, we could then set the list&#8217;s <b>onchange</b> event handler to call up the <b>FontName</b> command, like so:</p>
<pre>objEditor.execCommand("FontName", false, "Times New Roman");</pre>
<p>It&#8217;s pretty straightforward how it works.</p>
<h1>Further Inspiration</h1>
<p>There are so many possibilities, especially with the sheer number of commands available at your disposal. For example, there is the <b>2D-Position</b> command, which allows absolutely positioned elements to be moved by dragging. There is also the <b>CreateLink</b>, which adds a hyperlink or changes text into a hyperlink. <b>InsertButton</b>, <b>InsertImage</b> and<br />
<b>SaveAs</b> are just a microscopically small selection of commands also at your disposal. For a full list of commands available, check out <a href="http://msdn2.microsoft.com/en-us/library/ms533049(VS.85).aspx" target="_blank">http://msdn2.microsoft.com/en-us/library/ms533049(VS.85).aspx</a>.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/xylotdesign.wordpress.com/9/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/xylotdesign.wordpress.com/9/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/xylotdesign.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/xylotdesign.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/xylotdesign.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/xylotdesign.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/xylotdesign.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/xylotdesign.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/xylotdesign.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/xylotdesign.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/xylotdesign.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/xylotdesign.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=9&subd=xylotdesign&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://xylotdesign.wordpress.com/2008/03/17/js-wysisyg-rich-text-box/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b31bda9bb80f4c9b9f507a02539c79e7?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Xylot Design</media:title>
		</media:content>
	</item>
		<item>
		<title>PHP: 10 Security Mistakes &amp; Oversights</title>
		<link>http://xylotdesign.wordpress.com/2008/03/15/php-10-security-mistakes-oversights/</link>
		<comments>http://xylotdesign.wordpress.com/2008/03/15/php-10-security-mistakes-oversights/#comments</comments>
		<pubDate>Sun, 16 Mar 2008 00:32:34 +0000</pubDate>
		<dc:creator>Xylot Design</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[cross-site]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[mistakes]]></category>
		<category><![CDATA[oversights]]></category>
		<category><![CDATA[permissions]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sessions]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[upload]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://xylotdesign.wordpress.com/?p=8</guid>
		<description><![CDATA[In my years as a web developer, there are many security issues with which I have had to become familiar. There are a lot of people out their with malicious intent. One of them could come across your site and expose a flaw that allows them access to crucial information, or the entire site itself. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=8&subd=xylotdesign&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In my years as a web developer, there are many security issues with which I have had to become familiar. There are a lot of people out their with malicious intent. One of them could come across your site and expose a flaw that allows them access to crucial information, or the entire site itself. Don&#8217;t let this happen! Below are the <b>10</b> most common errors programmers make that could prove fatal.<span id="more-8"></span></p>
<h1>1. SQL Injection</h1>
<p>SQL injection is the act of maliciously adding SQL code that is then processed along with SQL code. This happens when the programmer neglects to properly check user input or query string variables before using it in SQL statements.</p>
<p>For example:</p>
<pre>$id = $_GET['id'];
$result = mysql_query("SELECT * FROM tblComments WHERE fldCommentID = $id");</pre>
<p>You may be asking yourself, &#8220;What&#8217;s wrong with the above code?&#8221; Plenty&#8230; just enough for any hacker to exploit it. What would happen if the visitor entered in the URL <b>page.php?id=0 UNION SELECT * FROM tblAdminUsers</b> ? Because the <b>$id</b> variable is not properly checked for injected code, it is added to the SQL query and processed. Here is what the resulting SQL query would look like:</p>
<pre>SELECT * FROM tblComments WHERE fldCommentID = 0 UNION SELECT * FROM tblAdminUsers</pre>
<p>It is a common enough table naming convention to give it a try, and the results would be damaging, exposing information you never intended to become public. Knowing that $id is supposed to be an integer, one approach to preventing injection is to get the integer value of the variable.</p>
<pre>$id = intval($_GET['id']);</pre>
<p>The above code would then only return a number, and therefore be safe.</p>
<p>There are many other methods of injection, however, so keep your eyes open for any possible vulnerabilities!</p>
<h1>2. Cross-Site Scripting</h1>
<p>Cross-site scripting, or XSS, is the act of inputting HTML or JavaScript code such that it is called and then run. Failing to properly check for HTML tags will allow this to happen. A good example is if you echo a variable taken from the query string. If the user altered the query string such that it included JavaScript code, the possibilities would be endless for hacking and other mischief.</p>
<pre>page.php?something=&lt;script&gt;alert("hello");&lt;/script&gt;</pre>
<p>Visiting the above URL could potentially run the script, as it could be echoed in its entirety. Once that doorway is open, anything is then possible. Suppose the visitor is posting a comment and include JavaScript in his comment. Without proper checking, it would affect anybody viewing the page on which the comment then appears.</p>
<p>One possible solution would be to escape all tag brackets (<b>&lt;</b> and <b>&gt;</b>) with their HTML entity equivalent (<b>&lt;</b> and <b>&gt;</b>) or, if there is no reason for anybody to be using such characters, replace them with nothing at all.</p>
<h1>3. GET Variable Manipulation</h1>
<p>If a site has a form on it that sends to another page via GET method (like PayPal does), it quite possible to go through all the hard work of constructing a URL and altering a variable or two in the process. Consider the following code:</p>
<pre>&lt;p&gt;Checkout below!&lt;/p&gt;
&lt;form action="checkout.php" method="get"&gt;
&lt;input type="hidden" name="grandtotal" value="1234.00" /&gt;
&lt;input type="submit" value="Checkout" /&gt;
&lt;/form&gt;</pre>
<p>If the visitor simply clicked on the <b>Checkout</b> button, she would then be taken to <b>checkout.php?grandtotal=1234.00</b> where, presumable, the site would then deduct $1,234.00 from a stored credit card or account balance of some sort, and so forth. One method of simple hacking would involve examining the form code, and then manually entering in a new URL: <b>checkout.php?grandtotal=5.00</b> where the visitor would then only have to pay $5.00. Such oversights could prove extremely costly.</p>
<p>To fix such a mistake, the grand total could be stored in a database or session variable, or passed via POST instead of GET; however, a better solution would be to double-check the grand total sent to checkout.php, comparing it to what it should be. It is well worth the extra effort and work to prevent losing thousands or even millions of dollar in revenue.</p>
<h1>4. System Calls</h1>
<p>The <b>exec()</b>, <b>passthru()</b> and <b>system()</b> functions all allow you to execte system commands from within your PHP code. Failing to check the commands processed by these functions can potentially result in hackers accessing private information by altering the input. Two function exist to assist in your fight against malicious destruction: <b>escapeshellarg()</b> and <b>escapeshellcmd()</b>.</p>
<p>The <b>escapeshellarg()</b> function removes potentially harmful parts of user input by adding single quotes around the string and escaping any single quotes within. It would be used like so:</p>
<pre>$command_fixed = escapeshellarg($command);</pre>
<p>The <b>escapeshellcmd()</b> function is similar to the former, except that it only escapes characters that have a special meaning to the operating system at hand.</p>
<h1>5. File Uploads</h1>
<p>When uploading content to a site, PHP creates a file but does not automatically check the validity of the file name, the file type, or the size. A user could create his own form specifying some other file to upload instead.</p>
<p>The functions <b>move_uploaded_file()</b> and <b>is_uploaded_file()</b> are useful to assist in solving this problem, but it is best to check the <b>$_FILES</b> global array to ensure the file size and type are allowable for your application.</p>
<h1>6. Includes</h1>
<p>PHP allows you to include external files as if it were within the page itself, and is useful for code that is used extensively, class definitions, and for better organization of your site. However, if the included files are dependent upon user input, this could prove to be a big mistake if not properly checked. If the user visits <b>index.php?p=article</b>, let&#8217;s say our <b>index.php</b> page runs the following:</p>
<pre>include_once($_GET['p'].'php');</pre>
<p>Then a page called article.php would be called up and run. If, however, the user visits <b>index.php?p=http://mysite.com/mycode</b>, then the code would include <b>http://mysite.com/mycode.php</b>, which could potentially contain malicious and destructive code. The code from that page would be run as though it were running on the site&#8217;s own server, which is never a good idea.</p>
<p>Even if you restricted all included files to begin with something like &#8220;include-&#8221; (so that <b>include-articles.php</b> would be called up instead), any hacker could go through the trouble of registering a domain name beginning with &#8220;include-&#8221; and doing the same thing as before.</p>
<p>The only solution is to check the query string variable for illegal characters, such as slashes, colons, and even periods.</p>
<p><u><b>NOTE:</b></u> Make sure all included files end in the proper extension, whether it&#8217;s .php for PHP or .aspx/.asp for ASP. Otherwise, if a visitor somehow finds out what files you are including, they could view a file such as <b>lib.inc</b>, as opposed to <b>lib.inc.php</b>, which would not expose the PHP code within.</p>
<h1>7. Unrestricted Access and Permissions</h1>
<p>If your site has an administration section, it is best to only allow certain IP addresses (ones you know you will be on when logging in). Another good idea an Apache <b>.htaccess</b> file with a username and password. This is a common mistake not native to PHP; it could happen to anybody.</p>
<p>On a similar note, never <i><b>EVER</b></i> back up PHP files by changing or adding an extension. This will most likely result in the PHP code being displayed in its raw format, exposed to the entire world, rather than parsed by the server. Servers usually only recognize certain extensions for certain languages (like .php for PHP, etc.) and any other extension wil display as an ASCII text file. NOT a good idea.</p>
<h1>8. Session IDs</h1>
<p>It is very easy for any hacker to hijack another user&#8217;s session, if they know that user&#8217;s session ID, and potentially see information that they really shouldn&#8217;t. While not completely avoidable, there are steps you can take to increase security.</p>
<p>One solution is to regenerate the session ID upon login using the <b>session_regenerate_id()</b> function. A hacker could try to set her own session ID prior to login; regenerating the session ID would prevent this action from doing anything significant.</p>
<h1>9. Error Reporting</h1>
<p>Before your site goes live, you should get rid of all <b>display_errors()</b> functions, as well as set the <b>display_errors</b> variable in your <b>php.ini</b> file to <b>0</b>. Errors that are displayed could reveal crucial information about your database structure, connection string, and other credentials.</p>
<p>If you still want to be able to view errors, you can set the <b>error_log</b> variable in <b>php.ini</b> to <b>1</b> and check your PHP error log frequently for caught errors. You can also create your own code to handle errors.</p>
<h1>10. Unvalidated Access to Protected Areas</h1>
<p>I&#8217;ve been guilty of this once before. If your site has an administration or members-only area, each and every single page that should be available to members only, should be protected properly.</p>
<p>At the top of every protected page, make sure that their login credentials are validated, and that the session or cookie variable you use for logging in users is still flagged and active. Otherwise, users would be able to view certain information without ever logging in. Nobody should be able to view any protected pages by bypassing the login screen.</p>
<h1>Conclusion</h1>
<p>There are so many ways that people can hack into your code and your site. The above covers just a handful of mistakes you could make in your code that would expose vulnerabilities and crucial information. Be safe, be informed, and be cautious!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/xylotdesign.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/xylotdesign.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/xylotdesign.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/xylotdesign.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/xylotdesign.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/xylotdesign.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/xylotdesign.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/xylotdesign.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/xylotdesign.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/xylotdesign.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/xylotdesign.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/xylotdesign.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=8&subd=xylotdesign&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://xylotdesign.wordpress.com/2008/03/15/php-10-security-mistakes-oversights/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b31bda9bb80f4c9b9f507a02539c79e7?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Xylot Design</media:title>
		</media:content>
	</item>
		<item>
		<title>AJAX: A Primer</title>
		<link>http://xylotdesign.wordpress.com/2008/03/14/ajax-a-primer/</link>
		<comments>http://xylotdesign.wordpress.com/2008/03/14/ajax-a-primer/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 18:33:07 +0000</pubDate>
		<dc:creator>Xylot Design</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[primer]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[yahoo]]></category>

		<guid isPermaLink="false">http://xylotdesign.wordpress.com/?p=5</guid>
		<description><![CDATA[First off, what is AJAX? AJAX stands for Asynchronous JavaScript And XML. What does that mean? Basically, it means that you can call up other scripts and retrieve dynamically generated content (or perform simple actions) without ever leaving the current page. It&#8217;s how a large number of sites let you log in nowadays, and on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=5&subd=xylotdesign&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>First off, what is AJAX? AJAX stands for <u><b>A</b></u>synchronous <u><b>J</b></u>avaScript <u><b>A</b></u>nd <u><b>X</b></u>ML. What does that mean? Basically, it means that you can call up other scripts and retrieve dynamically generated content (or perform simple actions) without ever leaving the current page. It&#8217;s how a large number of sites let you log in nowadays, and on what Yahoo! Mail Beta is based. It became popular through <a href="http://www.google.com/webhp?complete=1&amp;hl=en" target="_blank">Google Suggest</a>, where AJAX is the framework behind that little list that pops up below the search bar while you type in your query.<span id="more-5"></span></p>
<div style="text-align:center;"><img src="http://img.xylot.net/googlesuggest.jpg" alt="Google Suggest screenshot" border="0" height="350" width="400" /></div>
<p>In Google Suggest, AJAX calls up an external script while you type, looking through a list of popular search queries, and return those that start with the text you are typing, providing suggestions for your search. It is quite useful in that it allows the visitor to see a search query that might return more (or better) results than the one the visitor had in mind.</p>
<p>AJAX isn&#8217;t a programming language in itself; it is simply a method of combining JavaScript and the dynamic programming language of your choice (ASP, PHP, etc.) to run another page&#8217;s server-side script. There are two parts to an AJAX script: the originating page, where the user is located; and the external page, containing the script that AJAX calls.</p>
<p>We&#8217;re going to create our own little page with a &#8220;name&#8221; text field. When somebody types text into that field, the page will call up an external script that returns the current time. (Sure, we can use plain JS to do this, but this is just a demonstration!)</p>
<p>To start off, we will create the HTML page with the form.</p>
<pre>&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;AJAX Test Page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form name="testform"&gt;
            Name: &lt;input type="text" name="inputname" /&gt;&lt;br /&gt;
            Time: &lt;input type="text" name="currtime" /&gt;
        &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Pretty basic, right? So far we have the text &#8220;Name&#8221;, followed by a text field in which the user can enter text. Nothing else happens at the moment, as you can see.</p>
<p>We need to call up our script when the user types in text. The best handler to use for this is the <b>onkeyup</b> event handler. Add the event handler and have it call up a function we will then create:</p>
<pre>&lt;input type="text" name="inputname" onkeyup="getTime()" /&gt;</pre>
<p>Different browsers use different methods to create the XML-HTTP Request object we will need. Internet Explorer uses an ActiveX object, while most other browsers use a built-in JavaScript object. We will use <b>try</b> and <b>catch</b> statements to generate the correct object.</p>
<p>Let&#8217;s define the function that will return the XMLHttpRequest object. In the <b>&lt;head&gt;</b> section, add some JavaScript:</p>
<pre>&lt;script language="javascript" type="text/javascript"&gt;
    function getXHObject() {
        var xmlHttp = null;
        try {
            // For Firefox, Opera 8.0+, and Safari:
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            // Internet Explorer (below 7.0, and 7.0+):
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    }
&lt;/script&gt;</pre>
<p>The above script will attempt to create the built-in JS object, which will work for most non-IE browsers. If that doesn&#8217;t work, then it tries to call up one of the IE-supported methods. The first one works with versions of IE from 5.5 to under 7.0. The second one works with IE 7.0+. Finally, it returns the XML-HTTP object.</p>
<p>What if none of them work? In that case, the user&#8217;s browser is incredibly obsolete and does not support AJAX at all. Sure, this isn&#8217;t 100% compatible with all browsers, but so few people use ancient web browsers that it doesn&#8217;t matter much anyway. It comes to the point where they really should upgrade anyway, to be totally honest.</p>
<p>Next, we will create the <b>getTime()</b> function, which will call up the external script and retrieve the needed information.</p>
<pre>var xmlHttp;    // This var must be global
function getTime() {
    // If the input mattered, we would check for blank or invalid input here
    // Create XML-HTTP object
    xmlHttp = getXHObject();
    // Check if null - browser is obselete
    if (xmlHttp == null) {
        alert('Your browser does not support AJAX!');
        return;
    }
    // The URL we will call - if input mattered, we would add a query string of some sort.
    var url = 'gettime.php';
    // Required AJAX code
    xmlHttp.onreadystatechange = stateChanged; // Specify function that will output info.
    xmlHttp.open('GET', url, true);            // Open the URL for sending...
    xmlHttp.send(null);                        // ...and send the request.
}</pre>
<p>If the input affected the output (such as getting information based on user input), then the above <b>getTime()</b> function would have included the input as a parameter, and we would have checked the input to make sure it was valid. We would also have generated a URL with the input in the query string, such as <b>getusername.php?id=123</b>.</p>
<p>Next, we will define the <b>stateChanged()</b> function, which is called while the XML-HTTP request is working.</p>
<pre>function stateChanged() {
    if (xmlHttp.readyState == 4) {
        // Output was received
        document.testform.currtime.value = xmlHttp.responseText;
    } else {
        // Nothing was received yet, but the XML-HTTP request is working.
        // We may want to generate a message that asks the user to wait.
    }
}</pre>
<p>The above function will set the <b>currtime</b> text field&#8217;s value to the response text — the output received from the <b>gettime.php</b> external script.</p>
<p>Finally, we will set up the <b>gettime.php</b> page itself. Whatever we <b>echo</b> out will be the response text when called via AJAX. In a new file, we will use PHP to get the current time.</p>
<pre>&lt;?php
    echo date('g:i:s A');
    // Return something like "1:45:23 AM"
?&gt;</pre>
<p>And that&#8217;s it!</p>
<p>Basically, here&#8217;s how it works: The user types in a value into the &#8220;Name&#8221; field. After each keystroke, the <b>getTime()</b> function is called and creates an XML-HTTP object. This is then used to communicate silently with <b>gettime.php</b>, which outputs the time. This output triggers a Ready State of <b>4</b>. The <b>stateChanged()</b> function is then used to take the output and put it into the &#8220;Time&#8221; text field.</p>
<p>Now you know how to utilize AJAX, and have just increased your web development arsenal. As stated above, you can have output that changes depending on the user input. For example, you could have a text field that receives a User ID as input, and the external script will take that ID, query a database, and return that user&#8217;s full name, or some other piece of information.</p>
<p>Furthermore, instead of using <b>onkeyup</b> as the event handler, you could have a button click (<b>onclick</b>) or a click away from the text field (<b>onblur</b>) act as the trigger.</p>
<p>On a side note, I promise that future posts will not be as long as this one. I just wanted to get an AJAX primer out of the way, kind of as our starting big bang.</p>
<p><i>Happy coding!</i></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/xylotdesign.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/xylotdesign.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/xylotdesign.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/xylotdesign.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/xylotdesign.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/xylotdesign.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/xylotdesign.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/xylotdesign.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/xylotdesign.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/xylotdesign.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/xylotdesign.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/xylotdesign.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=5&subd=xylotdesign&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://xylotdesign.wordpress.com/2008/03/14/ajax-a-primer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b31bda9bb80f4c9b9f507a02539c79e7?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Xylot Design</media:title>
		</media:content>

		<media:content url="http://img.xylot.net/googlesuggest.jpg" medium="image">
			<media:title type="html">Google Suggest screenshot</media:title>
		</media:content>
	</item>
		<item>
		<title>Welcome!</title>
		<link>http://xylotdesign.wordpress.com/2008/03/14/welcome/</link>
		<comments>http://xylotdesign.wordpress.com/2008/03/14/welcome/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 02:16:12 +0000</pubDate>
		<dc:creator>Xylot Design</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://xylotdesign.wordpress.com/?p=3</guid>
		<description><![CDATA[Welcome to the blog of Xylot Design!
The purpose of this blog is to share code snippets, functions, programming methods, design ideas, and other innovations with the world. We&#8217;ll be posting informational and extremely useful pieces of code. Just wait until we show you what we have in store for you!
      [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=3&subd=xylotdesign&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Welcome to the blog of Xylot Design!</p>
<p>The purpose of this blog is to share code snippets, functions, programming methods, design ideas, and other innovations with the world. We&#8217;ll be posting informational and extremely useful pieces of code. Just wait until we show you what we have in store for you!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/xylotdesign.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/xylotdesign.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/xylotdesign.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/xylotdesign.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/xylotdesign.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/xylotdesign.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/xylotdesign.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/xylotdesign.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/xylotdesign.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/xylotdesign.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/xylotdesign.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/xylotdesign.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=3&subd=xylotdesign&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://xylotdesign.wordpress.com/2008/03/14/welcome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b31bda9bb80f4c9b9f507a02539c79e7?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Xylot Design</media:title>
		</media:content>
	</item>
	</channel>
</rss>