<?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>9tutorials - The best collection of tutorials &#187; Javascript</title>
	<atom:link href="http://9tutorials.com/tag/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://9tutorials.com</link>
	<description>Photoshop tutorials , Flash tutorials, PHP tutorials and much more</description>
	<lastBuildDate>Sat, 20 Feb 2010 20:24:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Create A Tabbed Interface Using jQuery</title>
		<link>http://9tutorials.com/2008/08/14/create-a-tabbed-interface-using-jquery.html</link>
		<comments>http://9tutorials.com/2008/08/14/create-a-tabbed-interface-using-jquery.html#comments</comments>
		<pubDate>Wed, 31 Dec 1969 17:00:00 +0000</pubDate>
		<dc:creator>kusanagi</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tabbed]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2008/08/14/create-a-tabbed-interface-using-jquery.html</guid>
		<description><![CDATA[Creating tabbed interfaces suddenly becomes a piece-of-cake when using the Tabs function in the jQuery UI library. It can be utilized to create completely unique interfaces without having to be a coding God &#8211; using only one line of code!
]]></description>
			<content:encoded><![CDATA[<p>Creating tabbed interfaces suddenly becomes a piece-of-cake when using the Tabs function in the jQuery UI library. It can be utilized to create completely unique interfaces without having to be a coding God &#8211; using only one line of code!</p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2008/08/14/create-a-tabbed-interface-using-jquery.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display content dynamically with AJAX</title>
		<link>http://9tutorials.com/2007/07/20/display-content-dynamically-with-ajax.html</link>
		<comments>http://9tutorials.com/2007/07/20/display-content-dynamically-with-ajax.html#comments</comments>
		<pubDate>Sat, 21 Jul 2007 00:52:38 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ajax]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/07/20/display-content-dynamically-with-ajax.html</guid>
		<description><![CDATA[We want to design a commenting system where visitors of your website can leave a comment on a news post. The form for submitting a comment should only be visible when a â€œpost comment buttonâ€ is clicked. The comment form should be displayed without reloading the web page.
The solution
To get this behaviour we are going [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p>We want to design a commenting system where visitors of your website can leave a comment on a news post. The form for submitting a comment should only be visible when a â€œpost comment buttonâ€ is clicked. <span id="more-2008"></span>The comment form should be displayed without reloading the web page.</p>
<p><strong>The solution</strong></p>
<p>To get this behaviour we are going to use AJAX (Asyncronous Javascript And XML) which is perfect for these kind of problems. With AJAX it is possible to send requests via Javascript that updates the webpage wiithout the need of a page refresh.</p>
<p>We start with the html code that we later will integrate with a Javascript to reveal the hidden textbox. Create a new file and call it comments.html.</p>
<p>[source:html]</p>
<p>&lt;div style=&#8217;width:400;border: 5px solid #000000;margin: 4 auto;padding:10px;&#8217;&gt;<br />
&lt;b&gt;My news post&lt;/b&gt;&lt;br&gt;<br />
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque ultrices magna eget<br />
nunc. Pellentesque consequat dui sit amet leo. Maecenas a est. Donec justo nunc,<br />
egestas vitae, consequat ac, aliquet ut, nisl. In suscipit nibh vitae lacus.<br />
Pellentesque sapien odio, imperdiet vitae, scelerisque quis, ultricies a, pede.<br />
Nullam feugiat<br />
&lt;br&gt;&lt;br&gt;<br />
&lt;a href=&#8221;javascript:showForm(&#8216;comment&#8217;);&#8221;&gt;Post your comment&lt;/a&gt;<br />
&lt;div id=&#8221;comment&#8221; style=&#8217;display:none&#8217;&gt;<br />
&lt;form id=&#8221;commentform&#8221; method=&#8221;post&#8221; action=&#8221;#&#8221; style=position: relative; z-index: 1&gt;<br />
&lt;textarea name=&#8221;newpost&#8221; rows=&#8221;5&#8243; cols=&#8221;45&#8243;&gt;Your comment here&lt;/textarea&gt;&lt;br&gt;<br />
&lt;input type=&#8221;submit&#8221; value=&#8221;SEND&#8221; class=&#8221;button&#8221; name=&#8221;postcomment&#8221;/&gt;<br />
&lt;/form&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>[/source]</p>
<p>Important things to note with this code is</p>
<p><em>javascript:showForm(&#8216;comment&#8217;);</em></p>
<p>that calls the javascript function ShowForm when the link is clicked on. Also note the code</p>
<p><em>&lt;div id=&#8221;comment&#8221; style=&#8217;display:none&#8217;&gt;</em></p>
<p>that creates a div with the id â€œcommentâ€ and hides this div by default.</p>
<p>With the html code out of the way we can move on to the javascript part that is supposed to reveal the hidden div when the â€œpost commentâ€ link is clicked. To do this we add the following code to our html file.</p>
<p>[source: javascript]</p>
<p>&lt;script&gt;<br />
function showForm(divID)<br />
{<br />
var elem, vis;<br />
if( document.getElementById ) // this is the standard way<br />
elem = document.getElementById(divID);<br />
else if( document.all ) // this is how older msie versions work<br />
elem = document.all[divID];<br />
else if( document.layers )// this is how netscape works<br />
elem = document.layers[divID];<br />
vis = elem.style;<br />
// if the style.display contains no value we try to figure it out<br />
if(vis.display==&#8221;&amp;&amp;elem.offsetWidth!=undefined&amp;&amp;elem.offsetHeight!=undefined)<br />
vis.display = (elem.offsetWidth!=0&amp;&amp;elem.offsetHeight!=0)?&#8217;block&#8217;:'none&#8217;;<br />
vis.display = (vis.display==&#8221;||vis.display==&#8217;block&#8217;)?&#8217;none&#8217;:'block&#8217;;<br />
}<br />
&lt;/script&gt;<br />
[/source]</p>
<p>That &#8217;s all. Simple, right ?</p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/07/20/display-content-dynamically-with-ajax.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Creating the Ajax application with Java</title>
		<link>http://9tutorials.com/2007/06/22/creating-the-ajax-application-with-java.html</link>
		<comments>http://9tutorials.com/2007/06/22/creating-the-ajax-application-with-java.html#comments</comments>
		<pubDate>Fri, 22 Jun 2007 07:27:44 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ajax]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/06/22/creating-the-ajax-application-with-java.html</guid>
		<description><![CDATA[We&#8217;ll begin with the complete HTML and JavaScript code for our first application, a simple web page that displays the decimal value of any character. Then we&#8217;ll break apart the JavaScript and examine it.

[source:html]&#60;html&#62;
&#60;head&#62;
&#60;link rel=&#8221;stylesheet&#8221; type=&#8221;text/css&#8221; href=&#8221;style.css&#8221;&#62;
&#60;SCRIPT language=&#8221;JavaScript&#8221; src=&#8221;ajax.js&#8221;&#62;&#60;/SCRIPT&#62;
&#60;title&#62;Ajax On Java, Chapter 2 Example&#60;/title&#62;
&#60;/head&#62;
&#60;body onload=&#8221;focusIn( );&#8221;&#62;
&#60;h1&#62; AJAX CHARACTER DECODER &#60;/h1&#62;
&#60;h2&#62; Press a key to find [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p>We&#8217;ll begin with the complete HTML and JavaScript code for our first application, a simple web page that displays the decimal value of any character. Then we&#8217;ll break apart the JavaScript and examine it.</p>
<p><span id="more-1345"></span></p>
<p>[source:html]&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;link rel=&#8221;stylesheet&#8221; type=&#8221;text/css&#8221; href=&#8221;style.css&#8221;&gt;<br />
&lt;SCRIPT language=&#8221;JavaScript&#8221; src=&#8221;ajax.js&#8221;&gt;&lt;/SCRIPT&gt;<br />
&lt;title&gt;Ajax On Java, Chapter 2 Example&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body onload=&#8221;focusIn( );&#8221;&gt;<br />
&lt;h1&gt; AJAX CHARACTER DECODER &lt;/h1&gt;<br />
&lt;h2&gt; Press a key to find its value. &lt;/h2&gt;<br />
&lt;table&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;<br />
Enter Key Here -&gt;<br />
&lt;input type=&#8221;text&#8221; id=&#8221;key&#8221; name=&#8221;key&#8221;<br />
onkeyup=&#8221;convertToDecimal( );&#8221;&gt;</p>
<p>&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;br /&gt;<br />
&lt;table&gt;<br />
&lt;tr&gt;<br />
&lt;td colspan=&#8221;5&#8243; style=&#8221;border-bottom:solid black 1px;&#8221;&gt;<br />
Key Pressed:<br />
&lt;input type=&#8221;text&#8221; readonly id=&#8221;keypressed&#8221;&gt;<br />
&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt; Decimal &lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;&lt;input type=&#8221;text&#8221; readonly id=&#8221;decimal&#8221;&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;[/source]</p>
<p>For the most part, this is standard HTML. There are only two JavaScript references: focusIn( ) and convertToDecimal( ). The focusIn( ) function merely puts the cursor in the right input field when the page loads, so the user doesn&#8217;t have to move it there with the mouse.</p>
<p>The convertToDecimal( ) function will be our entry into the Ajax world. The lays out the JavaScript code that supports our web page, ajax.js.</p>
<p>[source:javascript]var req;</p>
<p>function convertToDecimal( ) {<br />
var key = document.getElementById(&#8220;key&#8221;);<br />
var keypressed = document.getElementById(&#8220;keypressed&#8221;);<br />
keypressed.value = key.value;<br />
var url = &#8220;/ajaxdecimalcodeconverter/response?key=&#8221; + escape(key.value);<br />
if (window.XMLHttpRequest) {<br />
req = new XMLHttpRequest( );<br />
}<br />
else if (window.ActiveXObject) {<br />
req = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
}<br />
req.open(&#8220;Get&#8221;,url,true);<br />
req.onreadystatechange = callback;<br />
req.send(null);<br />
}</p>
<p>function callback( ) {<br />
if (req.readyState==4) {<br />
if (req.status == 200) {<br />
var decimal = document.getElementById(&#8216;decimal&#8217;);<br />
decimal.value = req.responseText;<br />
}<br />
}<br />
clear( );<br />
}<br />
function clear( ) {<br />
var key = document.getElementById(&#8220;key&#8221;);<br />
key.value=&#8221;";<br />
}<br />
function focusIn( ) {<br />
document.getElementById(&#8220;key&#8221;).focus( );<br />
}[/source]</p>
<p>Let&#8217;s take a look at convertToDecimal( ), which is our entry point from index.html. The main JavaScript object we&#8217;ll use is XMLHttpRequest. Unfortunately, one problem with JavaScript is that the code isn&#8217;t the same on all browsers. In Mozilla, Firefox, and Safari, we get an XMLHttpRequest object like this:</p>
<p><em>new XMLHttpRequest( );</em></p>
<p>In Internet Explorer, we use an ActiveX object:</p>
<p><em>new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);</em></p>
<p>Because we can&#8217;t tell in advance which browsers users will view our web page with, we have to write code that will work on any of the likely candidates. First, we must determine whether the user is using Internet Explorer or some other browser, such as Firefox or Mozilla. This task is handled by the following code:</p>
<p>[source:javascript]if (window.XMLHttpRequest) {<br />
req = new XMLHttpRequest( );<br />
}<br />
else if (window.ActiveXObject) {<br />
req = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
}[/source]</p>
<p>That&#8217;s basically it: req is now an object that we can use to build our Ajax page.</p>
<p>Now let&#8217;s look at some code that does some real work. We will be using the code from axax.js in the next chapter, so examine it closely and pay special attention to the mechanism that talks to the server. Since we&#8217;re Java developers, the backend will be a servlet, but the web page doesn&#8217;t care.</p>
<p>The convertToDecimal( ) function first gets a String from the form and then sets the url variable to &#8220;/ajaxdecimalcodeconverter/response?key=&#8230;&#8221;. Eventually, we&#8217;ll send this URL to the server (in our case, a servlet) and expect a response (the decimal value of the key), but we&#8217;re not going to send it in response to a Submit button press; we&#8217;re going to send it asynchronously (that is, as soon as we have the keystroke that we want to convert).</p>
<p>After the if/else block, where we figure out which browser is being used and get an appropriate req object, we open a connection to the servlet with the call:</p>
<p><em>req.open(&#8220;Get&#8221;,url,true);</em></p>
<p>Let&#8217;s look at the three parameters in the req.open( ) function:</p>
<p><strong>&#8220;Get&#8221; &#8211; </strong>The first parameter tells JavaScript whether to submit the request to the server using HTTPPost( ) or HTTPGet( ). The HTTPPost( ) method hides the parameters in the request; the HTTPGet( ) method puts the parameters in the URL for everyone to see. For this example, I chose HTTPGet( ) because it is easier to see what parameters are being passed, and the number of parameters is relatively small. If I were sending a complex set of parameters, I&#8217;d use &#8220;Post&#8221; instead.[*]</p>
<p>[*] I&#8217;m getting quite a ways ahead of the story, but it&#8217;s a good idea to use Get only when the request doesn&#8217;t make any changes to the data on the server. That&#8217;s clearly the case here. Conversely, it&#8217;s a bad idea to use Get when you are changing data on the server (for example, if you&#8217;re sending new data, or deleting existing data); in this case, use Post instead.</p>
<p><strong>url -</strong> The second parameter is the URL we&#8217;re passing to the server. We created that URL earlier in the method.</p>
<p><strong>true -</strong> The last parameter determines whether or not the call is asynchronous. When this parameter is TRue, the request is sent asynchronously. When designing Ajax applications, you always want to set the asynchronous flag to TRue; basically, it means &#8220;don&#8217;t stop anything, just notify me when the data comes back.&#8221;</p>
<p>Now, notice the next statement:</p>
<p><em>req.onreadystatechange=callback;</em></p>
<p>This line allows us to use the call asynchronously. We&#8217;re telling the req object to call the callback( ) function whenever a state transition occurs. Therefore, we can process data coming back from the server as soon as it arrives; whenever something happens, we&#8217;ll be notified.</p>
<p><em>What Is a Callback?<br />
A callback is executable code that is passed as a parameter to another function. In our example, we pass code to the XMLHTTPRequest object, which tells us what function to call when it is ready.</em></p>
<p><em>The JavaScript code generates a request that is sent to a servlet. When the servlet returns with the information, the callback function is invoked; in turn, the callback function can display the new information to the user. We specified which function to call with the following JavaScript code:</em></p>
<p><em>req.onreadystatechange = callback;</em></p>
<p><em>This is really powerful. There&#8217;s no more waiting on the page; when the data returns, the user will see it without having to wait for a page reload.</em></p>
<p>The last statement of convertToDecimal( ) sends the request:</p>
<p>req.send(null);</p>
<p>Now, let&#8217;s look at the callback( ) function:</p>
<p>[source:javascript]function callback( ) {<br />
if (req.readyState==4) {<br />
if (req.status == 200) {<br />
if (window.XMLHttpRequest) {<br />
nonMSPopulate( );<br />
}<br />
else if (window.ActiveXObject) {<br />
msPopulate( );<br />
}<br />
}<br />
}<br />
clear( );<br />
}[/source]</p>
<p>This function checks the readyState and the status returned by the server. The readyState can have one of five values, listed in table below</p>
<table cellpadding="4" cellspacing="0" frame="hsides" rules="all" width="100%">
<thead>
<th class="thead" scope="col" align="left" valign="bottom">
<p class="docText">Value</p>
</th>
<th class="thead" scope="col" align="left" valign="bottom">
<p class="docText">State</p>
</th>
<tr>
<td class="docTableCell" align="left" valign="top">
<p class="docText">0</p>
</td>
<td class="docTableCell" align="left" valign="top">
<p class="docText">Uninitialized</p>
</td>
</tr>
<tr>
<td class="docTableCell" align="left" valign="top">
<p class="docText">1</p>
</td>
<td class="docTableCell" align="left" valign="top">
<p class="docText">Loading</p>
</td>
</tr>
<tr>
<td class="docTableCell" align="left" valign="top">
<p class="docText">2</p>
</td>
<td class="docTableCell" align="left" valign="top">
<p class="docText">Loaded</p>
</td>
</tr>
<tr>
<td class="docTableCell" align="left" valign="top">
<p class="docText">3</p>
</td>
<td class="docTableCell" align="left" valign="top">
<p class="docText">Interactive</p>
</td>
</tr>
<tr>
<td class="docTableCell" align="left" valign="top">
<p class="docText">4</p>
</td>
<td class="docTableCell" align="left" valign="top">
<p class="docText">Complete</p>
</td>
</tr>
</thead>
</table>
<p>The callback( ) function is called on every state change, but that&#8217;s not exactly what we want. We don&#8217;t want to do anything until our request has completed, so we wait until req.readyState == 4.</p>
<p>The next check, req.status == 200, ensures that the HTTPRequest returned a status of OK (200). If the page is not found, status will equal 404. In this example, the code should be activated only when the request has been completed. Note that a readyState of 4 doesn&#8217;t tell us that the request completed correctly; all we know is that it completed. We still have to check the req.status code.</p>
<p><strong>How Is Our JavaScript Function Called?</strong></p>
<p>We&#8217;ve written a nice JavaScript function, convertToDecimal( ), that does some interesting things: it sends a request to the server without intervention by the user, and it arranges for the server&#8217;s response to be added to the web page. But how does convertToDecimal( ) get called? The browser calls it when it detects the keyup event on the &#8220;Enter Key Here -&gt;&#8221; input field. Here is the complete HTML for the input field:</p>
<p><em>&lt;input type=&#8221;text&#8221; id=&#8221;key&#8221; name=&#8221;key&#8221; onkeyup=&#8221;convertToDecimal( );&#8221;&gt;</em></p>
<p>onkeyup=&#8221;convertToDecimal( );&#8221; tells the browser to call the JavaScript function convertToDecimal( ) whenever the user presses and releases a key in the input field.</p>
<p><strong>How Do We Get the Value of the Key Pressed?</strong></p>
<p>Once control is passed to convertToXML( ), we make this call:</p>
<p><em>var key = document.getElementById(&#8220;key&#8221;);</em></p>
<p>At this point, the object with the id of key contains the decimal value of the key that was pressed. All that&#8217;s left for us to do is retrieve the value that the object named key contains. This value is kept in the value parameter of the key element, so key.value contains the value of the key that was pressed.</p>
<p>Once we&#8217;ve retrieved it, we want to place this value in a field for display. That allows us to clear the field used to enter the key. We&#8217;ve named the field for displaying the key keypressed. Here&#8217;s how to retrieve the keypressed field:</p>
<p><em>var keypressed = document.getElementById(&#8220;keypressed&#8221;);</em></p>
<p>The next step is to put the value of key into the value of keypressed:</p>
<p><em>keypressed.value = key.value;</em></p>
<p><strong>Formatting the Page</strong></p>
<p>The final step in developing our application is to create a CSS file to give the page some formatting</p>
<p>[source:css]body {<br />
font-family: Arial, Helvetica, sans-serif;<br />
font-size: small;<br />
text-align:center;<br />
background:#cbdada;<br />
}<br />
#keypressed{<br />
width:30;<br />
border:none;<br />
}<br />
#key {<br />
width:20px;<br />
padding:0;<br />
margin:0;<br />
border:none;<br />
text-align:left<br />
}<br />
h1, h2 {<br />
font-size:120%;<br />
text-align:center;<br />
}<br />
h2 {<br />
font-size:110%<br />
}<br />
table, input {<br />
margin-left:auto;<br />
margin-right:auto;<br />
padding:0px 10px;<br />
text-align:center;<br />
color:black;<br />
text-align:center;<br />
background: #a0f6f5;<br />
border:solid black 1px;<br />
}<br />
td {<br />
margin:10px 10px;<br />
padding: 0px 5px;<br />
border: none;<br />
}<br />
input {<br />
width: 80;<br />
border: none;<br />
border-top:solid #999999 1px;<br />
font-size: 80%;<br />
color: #555555;<br />
}[/source]</p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/06/22/creating-the-ajax-application-with-java.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>AJAX Generic Form Parser &#8211; With Validation</title>
		<link>http://9tutorials.com/2007/05/24/ajax-generic-form-parser-with-validation.html</link>
		<comments>http://9tutorials.com/2007/05/24/ajax-generic-form-parser-with-validation.html#comments</comments>
		<pubDate>Thu, 24 May 2007 16:35:06 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/05/24/ajax-generic-form-parser-with-validation.html</guid>
		<description><![CDATA[In this tutorial Iâ€™ll show you a simple method to pass any HTML form through AJAX without the need to hard code all form fields into the JavaScript or Server Side Script. Using this simple piece of JavaScript you can reuse it as is with any form, saving a lot of time. Iâ€™ve even added [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p>In this tutorial Iâ€™ll show you a simple method to pass any HTML form through AJAX without the need to hard code all form fields into the JavaScript or Server Side Script.<span id="more-419"></span> Using this simple piece of JavaScript you can reuse it as is with any form, saving a lot of time. Iâ€™ve even added basic validation to certain form element types (which would be expected).</p>
<p>Okay, straight to the main JS code.</p>
<p>[source:javascript]</p>
<p>var req = createXMLHttpRequest();</p>
<p>function createXMLHttpRequest() {<br />
var ua;<br />
if(window.XMLHttpRequest) {<br />
try {<br />
ua = new XMLHttpRequest();<br />
} catch(e) {<br />
ua = false;<br />
}<br />
} else if(window.ActiveXObject) {<br />
try {<br />
ua = new ActiveXObject(â€?Microsoft.XMLHTTPâ€?);<br />
} catch(e) {<br />
ua = false;<br />
}<br />
}<br />
return ua;<br />
}</p>
<p>function sendRequest(frm, file) {<br />
var rnd982g = Math.random();<br />
var str = â€œâ€?;<br />
if(str = getForm(frm)) {<br />
req.open(â€™GETâ€™, file+â€™?&#8217;+str+â€™&amp;rnd982g=â€™+rnd982g);<br />
req.onreadystatechange = handleResponse;<br />
req.send(null);<br />
}<br />
return false;<br />
}</p>
<p>function handleResponse() {<br />
if(req.readyState == 4){<br />
var response = req.responseText;<br />
document.getElementById(â€?resultsâ€?).innerHTML = response;<br />
}<br />
}</p>
<p>function getForm(fobj) {<br />
var str = â€œâ€?;<br />
var ft = â€œâ€?;<br />
var fv = â€œâ€?;<br />
var fn = â€œâ€?;<br />
var els = â€œâ€?;<br />
for(var i = 0;i &lt; fobj.elements.length;i++) {<br />
els = fobj.elements[i];<br />
ft = els.title;<br />
fv = els.value;<br />
fn = els.name;<br />
switch(els.type) {<br />
case &#8220;text&#8221;:<br />
case &#8220;hidden&#8221;:<br />
case &#8220;password&#8221;:<br />
case &#8220;textarea&#8221;:<br />
// is it a required field?<br />
if(encodeURI(ft) == &#8220;required&#8221; &amp;&amp; encodeURI(fv).length &lt; 1) {<br />
alert(&#8221;&#8217;+fn+&#8221; is a required field, please complete.&#8217;);<br />
els.focus();<br />
return false;<br />
}<br />
str += fn + &#8220;=&#8221; + encodeURI(fv) + &#8220;&amp;&#8221;;<br />
break;</p>
<p>case &#8220;checkbox&#8221;:<br />
case &#8220;radio&#8221;:<br />
if(els.checked) str += fn + &#8220;=&#8221; + encodeURI(fv) + &#8220;&amp;&#8221;;<br />
break;</p>
<p>case &#8220;select-one&#8221;:<br />
str += fn + &#8220;=&#8221; +<br />
els.options[els.selectedIndex].value + &#8220;&amp;&#8221;;<br />
break;<br />
} // switch<br />
} // for<br />
str = str.substr(0,(str.length &#8211; 1));<br />
return str;<br />
}<br />
[/source]</p>
<p>The JavaScript code above is split into 4 functions, here is an overview of what each function does.</p>
<p>createXMLHttpRequest() &#8211; This is the function which will establish the AJAX connection object, this is called as soon as the JS file is loaded.</p>
<p>sendRequest() &#8211; This function is the one which is called when the form is submitted. This function requires (passed in) the â€˜Form Objectâ€™ and the â€˜File Nameâ€™ of the script which will receive the form data. When this function has been called, it takes the form object and passes it to the JS function getForm() which in turn parses the whole form extracting all the data. sendRequest() then takes the open AJAX connection and passes all the data to the PHP file form processing. Finally when the request from the PHP server script is returned, it writes out the reply to an element on the screen with the div ID â€˜resultsâ€™.<br />
<!--adsense#inside--></p>
<p>getForm() &#8211; This is the magic, this function reads through the whole form and extracts the field data before returning it back to sendRequest(). It handles radio, text, password, textarea, select and checkbox field types. It even looks for validation requests on text, password and textarea fields (which is enabled by using title=â€?requiredâ€? in the form element).</p>
<p>Simple :)</p>
<p>Next is a very simple form with a mixture of field types and validation requests (nothing fancy here). Also included at the bottom is the div which prints out the results to the browser.</p>
<p>[source:html]</p>
<p>&lt;form&gt;<br />
Option One:<br />
&lt;input name=&#8221;radiobutton&#8221; type=&#8221;radio&#8221; value=&#8221;opt1&#8243;&gt;<br />
Option Two:<br />
&lt;input name=&#8221;radiobutton&#8221; type=&#8221;radio&#8221; value=&#8221;opt2&#8243;&gt;<br />
Option Three:<br />
&lt;input name=&#8221;radiobutton&#8221; type=&#8221;radio&#8221; value=&#8221;opt3&#8243;&gt;<br />
Text One*:<br />
&lt;input type=&#8221;text&#8221; name=&#8221;textOne&#8221; title=&#8221;required&#8221;&gt;<br />
Text Two:<br />
&lt;input type=&#8221;text&#8221; name=&#8221;textTwo&#8221;&gt;</p>
<p>&lt;select name=&#8221;mySelect&#8221;&gt;<br />
&lt;option value=&#8221;selectedOne&#8221;&gt;Select One&lt;/option&gt;<br />
&lt;option value=&#8221;selectedOne&#8221;&gt;Select One&lt;/option&gt;<br />
&lt;/select&gt;</p>
<p>&lt;input type=&#8221;button&#8221; value=&#8221; go &#8221; onClick=&#8221;sendRequest(this.form, &#8216;process.php&#8217;)&#8221;&gt;<br />
&lt;/form&gt;<br />
&lt;div id=&#8221;results&#8221; /&gt;<br />
[/source]</p>
<p>As you can see the form trigger which calls the AJAX actions is set in the button using the onClick method. Also note Iâ€™ve added title=â€?requiredâ€? to the â€˜textOneâ€™ field &#8211; this means the user will not be able to submit the form unless this field contains a value.</p>
<p>Finally here is a very stripped down Server script in PHP which takes the form values and returns them formatted back to the JS (you could do anything with the data at this stage!).</p>
<p>process.php</p>
<p>[source:php]</p>
<p>if(isset($_GET[â€™rnd982gâ€™])) {<br />
foreach($_GET as $a =&gt; $b) {<br />
if($a == â€œrnd982gâ€?) {<br />
// ignore this random number<br />
// only used to prevent browser caching<br />
} else {<br />
echo â€œ&lt;b&gt;$a&lt;/b&gt;: â€œ.stripslashes(htmlentities($b)).â€? â€œ;<br />
} // if<br />
} //foreach<br />
die;<br />
}<br />
[/source]</p>
<p>You could easily change the above to generate an email or add the form data to a database, the above example will echo out the results which are passed back to the div tag below the form.</p>
<p>There you have it, short and sweet (I hope) &#8211; Iâ€™ve intentionally kept the example scripts to a minumum but with still enough to be useful. Please feel free to take this and mess around with it.</p>
<p>Have Fun!!</p>
<p><em>Copyright @ Ajax Tutorial 2007</em></p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/05/24/ajax-generic-form-parser-with-validation.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to Develop Web Applications with Ajax</title>
		<link>http://9tutorials.com/2007/05/21/how-to-develop-web-applications-with-ajax.html</link>
		<comments>http://9tutorials.com/2007/05/21/how-to-develop-web-applications-with-ajax.html#comments</comments>
		<pubDate>Mon, 21 May 2007 20:39:39 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[web-development]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/05/21/how-to-develop-web-applications-with-ajax.html</guid>
		<description><![CDATA[In the past, web applications were limited because a web page had to be reloaded (or another page loaded in its place) in order for new data to be obtained. Other methods were available (without loading another page), but the techniques werenâ€™t well supported and had a tendency to be buggy. Recently, a technique that [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p>In the past, web applications were limited because a web page had to be reloaded (or another page loaded in its place) in order for new data to be obtained. Other methods were available (without loading another page), but the techniques werenâ€™t well supported and had a tendency to be buggy. <span id="more-349"></span>Recently, a technique that had not been widely supported in the past has become available to a large number of web surfers, giving developers more freedom to develop cutting-edge web applications. These applications, which asynchronously retrieve XML data via JavaScript, are affectionately known as â€œAjax applicationsâ€? (Asynchronous Javascript and XML applications). In this article, I will explain how to retrieve a remote XML file via Ajax to update a web page, and as this series continues, I will discuss more ways that Ajax technology can be used to take your web applications to the next level.</p>
<p>The first step is to create an XML file with some data. Weâ€™ll call this file data.xml. Itâ€™s a simple XML file and would most certainly be more complex in a real-world application, but for clarity our examples will be simple and concise.</p>
<p>[source:xml]</p>
<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?&gt;<br />
&lt;root&gt;<br />
&lt;data&gt;</p>
<p>This is some sample data. It is stored in an XML file and retrieved by JavaScript.</p>
<p>&lt;/data&gt;<br />
&lt;/root&gt;</p>
<p>[/source]</p>
<p>Now letâ€™s create a simple web page containing some sample data. This is the page that all our JavaScript will be in, and the page that users will visit to see the Ajax script in action. Letâ€™s call this file ajax.html.</p>
<p>[source:html]</p>
<p>&lt;!DOCTYPE html PUBLIC &#8220;-//W3C//DTD HTML 4.01//EN&#8221;<br />
&#8220;http://www.w3.org/TR/html4/strict.dtd&#8221;&gt;<br />
&lt;html lang=&#8221;en&#8221; dir=&#8221;ltr&#8221;&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text/html; charset=iso-8859-1&#8243;&gt;<br />
&lt;title&gt;Developing Web Applications with Ajax &#8211; Example&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;Developing Web Applications with Ajax&lt;/h1&gt;<br />
&lt;p&gt;This page demonstrates the use of Asynchronous Javascript and XML (Ajax) technology to<br />
update a web page&#8217;s content by reading from a remote file dynamically &#8212; no page reloading<br />
is required. Note that this operation does not work for users without JavaScript enabled.&lt;/p&gt;<br />
&lt;p id=&#8221;xmlObj&#8221;&gt;<br />
This is some sample data. It is the default data for this web page. &lt;a href=&#8221;data.xml&#8221;<br />
title=&#8221;View the XML data.&#8221; onclick=&#8221;ajaxRead(&#8216;data.xml&#8217;); this.style.display=&#8217;none&#8217;; return false&#8221;&gt;View XML     data.&lt;/a&gt;<br />
&lt;/p&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p>
<p>[/source]<br />
<!--adsense#inside--><br />
Note that we link to the data.xml file for users without JavaScript. For users with JavaScript, the function â€œajaxReadâ€? is called, the link is hidden, and the link does not redirect to the data.xml file. The function â€œajaxReadâ€? isnâ€™t defined yet, so if you test the example code above, youâ€™ll get a JavaScript error. Letâ€™s go ahead and define that function (and another) so you can see how Ajax works. The following SCRIPT goes in your HEAD tags:</p>
<p>[source:javascript]</p>
<p>&lt;script type=&#8221;text/javascript&#8221;&gt;&lt;!&#8211;<br />
function ajaxRead(file){<br />
var xmlObj = null;<br />
if(window.XMLHttpRequest){<br />
xmlObj = new XMLHttpRequest();<br />
} else if(window.ActiveXObject){<br />
xmlObj = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
} else {<br />
return;<br />
}<br />
xmlObj.onreadystatechange = function(){<br />
if(xmlObj.readyState == 4){<br />
updateObj(&#8216;xmlObj&#8217;, xmlObj.responseXML.getElementsByTagName(&#8216;data&#8217;)[0].firstChild.data);<br />
}<br />
}<br />
xmlObj.open (&#8216;GET&#8217;, file, true);<br />
xmlObj.send (&#8221;);<br />
}<br />
function updateObj(obj, data){<br />
document.getElementById(obj).firstChild.data = data;<br />
}<br />
//&#8211;&gt;&lt;/script&gt;</p>
<p>[/source]</p>
<p>Thatâ€™s quite a bit, so letâ€™s take this one piece at a time. The first function is â€œajaxReadâ€? â€“ what we call from our â€œView XML dataâ€? link on the web page. In the function, we define an â€œxmlObjâ€? variable â€“ this will be the middleman between the client (user viewing the web page) and the server (the web site itself). We define this object in an if/else chunk:</p>
<p>[source:javascript]</p>
<p>if(window.XMLHttpRequest){<br />
xmlObj = new XMLHttpRequest();<br />
} else if(window.ActiveXObject){<br />
xmlObj = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
} else {<br />
return;<br />
}</p>
<p>[/source]</p>
<p>This is just a test for the availability of different objects â€“ some browsers implement the XMLHttpRequest object differently, so when we define â€œxmlObjâ€? as our XMLHttpRequest object, we have to define it depending on what browser implementation is available. If no XMLHttpRequest object is available, we end the function with a â€œreturnâ€? statement to avoid errors. Most of the time, this check will return an XMLHttpRequest object â€“ this particular code should work in almost every browser out there, with the exception of some older browsers (it works in IE 5.01, but will cease to function in Netscape 4).</p>
<p>Next is this block:</p>
<p>[source:javascript]</p>
<p>xmlObj.onreadystatechange = function(){<br />
if(xmlObj.readyState == 4){<br />
updateObj(&#8216;xmlObj&#8217;, xmlObj.responseXML.getElementsByTagName(&#8216;data&#8217;)[0].firstChild.data);<br />
}<br />
}</p>
<p>[/source]</p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/05/21/how-to-develop-web-applications-with-ajax.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating sortable lists with PHP and Ajax</title>
		<link>http://9tutorials.com/2007/05/12/creating-sortable-lists-with-php-and-ajax.html</link>
		<comments>http://9tutorials.com/2007/05/12/creating-sortable-lists-with-php-and-ajax.html#comments</comments>
		<pubDate>Sat, 12 May 2007 20:32:50 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/05/12/creating-sortable-lists-with-php-and-ajax.html</guid>
		<description><![CDATA[You might have been in a situation before where you had a list of items in your database that needed to be output in a specific order. These items could be anything: perhaps a listing of your favourite movies or your favourite books. For whatever reason, you want them ordered in a custom way that [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p>You might have been in a situation before where you had a list of items in your database that needed to be output in a specific order. These items could be anything: perhaps a listing of your favourite movies or your favourite books.<span id="more-46"></span> For whatever reason, you want them ordered in a custom way that canâ€™t be determined automatically (such as alphabetical).</p>
<p>This article covers the implementation of a system that lets you easily define the order of such a list.</p>
<p>Traditionally, implementations of such functionality involve you clicking a â€œmove upâ€?, â€œmove downâ€?, â€œmove to topâ€?, or â€œmove to bottomâ€? button that switches the order the items (one item at a time). Or perhaps each item has a text box with a number in it, that by changing the numbers you can change the order of the list.</p>
<p>To achieve the Ajax effects (that is, the drag/drop effect, and the seamless saving of ordering data), we will be using the Prototype and Scriptaculous libraries.</p>
<p>Firstly, we will create a database table (compatible with MySQL and PostgreSQL) and populate it with data. Then we will output our list and apply the drag and drop effects to it. Finally, we will deal with the saving of the new ordering data.</p>
<p>For our example, we will use a list of â€œfavourite moviesâ€?, and implement functionality to change the order of our movies.</p>
<h3>Creating our database and populating it</h3>
<p>We will now create the database table we need in order to create this example. We wonâ€™t be writing all the code for inserting, editing and deleting of data, as it is beyond the scope of this example. As such, we will simply provide insert statements to create a static list of data.<br />
<!--adsense#inside--><br />
The examples below are for PostgreSQL and MySQL.</p>
<h3>Create your database</h3>
<p>First up, you need to create a database for this article. This may be in either PostgreSQL or MySQL. Additionally, you may need to setup a username and password to access the database, depending on your system setup.</p>
<h3>MySQL database schema</h3>
<p>[source:sql]</p>
<p>create table movies (<br />
movie_id    int             not null    auto_increment,<br />
title       varchar(255)    not null,<br />
ranking     int,</p>
<p>primary key (movie_id)<br />
);</p>
<p>[/source</p>
<h3>MySQL database schema</h3>
<p>[source:sql]</p>
<p><span class="hl-reserved">insert</span><span class="hl-code"> </span><span class="hl-reserved">into</span><span class="hl-code"> </span><span class="hl-identifier">movies</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">values</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&#8216;</span><span class="hl-string">American Pie</span><span class="hl-quotes">&#8216;</span><span class="hl-brackets">)</span>;<br />
<span class="hl-code"></span><span class="hl-reserved">insert</span><span class="hl-code"> </span><span class="hl-reserved">into</span><span class="hl-code"> </span><span class="hl-identifier">movies</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">values</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&#8216;</span><span class="hl-string">Die Hard</span><span class="hl-quotes">&#8216;</span><span class="hl-brackets">)</span>;<br />
<span class="hl-code"></span><span class="hl-reserved">insert</span><span class="hl-code"> </span><span class="hl-reserved">into</span><span class="hl-code"> </span><span class="hl-identifier">movies</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">values</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&#8216;</span><span class="hl-string">Clerks</span><span class="hl-quotes">&#8216;</span><span class="hl-brackets">)</span>;<br />
<span class="hl-code"></span><span class="hl-reserved">insert</span><span class="hl-code"> </span><span class="hl-reserved">into</span><span class="hl-code"> </span><span class="hl-identifier">movies</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">values</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&#8216;</span><span class="hl-string">Air Force One</span><span class="hl-quotes">&#8216;</span><span class="hl-brackets">)</span>;<br />
<span class="hl-code"></span><span class="hl-reserved">insert</span><span class="hl-code"> </span><span class="hl-reserved">into</span><span class="hl-code"> </span><span class="hl-identifier">movies</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">values</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&#8216;</span><span class="hl-string">Titanic</span><span class="hl-quotes">&#8216;</span><span class="hl-brackets">)</span>;<br />
<span class="hl-reserved">insert</span><span class="hl-code"> </span><span class="hl-reserved">into</span><span class="hl-code"> </span><span class="hl-identifier">movies</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">values</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&#8216;</span><span class="hl-string">The Shawshank Redemption</span><span class="hl-quotes">&#8216;</span><span class="hl-brackets">)</span>;<br />
<span class="hl-code"></span><span class="hl-reserved">insert</span><span class="hl-code"> </span><span class="hl-reserved">into</span><span class="hl-code"> </span><span class="hl-identifier">movies</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">values</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&#8216;</span><span class="hl-string">Gone In 60 Seconds</span><span class="hl-quotes">&#8216;</span><span class="hl-brackets">)</span><span class="hl-code">;</span></p>
<p>[/source ]</p>
<h3>About the schema</h3>
<p>The database table is fairly simple, it just consists of an ID, a movie title, and a field to store the ordering. Thereâ€™s no particular reason why the ranking field is allowed to be null, other than the values wonâ€™t be set when we initially insert our data.</p>
<p>If we were being really tricky, we would write a trigger on the database that would assign the next ranking value when a row is inserted, but that is beyond the scope of this article.</p>
<h3>Outputting the database data</h3>
<p>Now that weâ€™ve made and populated our database, weâ€™re going to write a PHP script to connect to this database and select all of this data. Because we are making both a MySQL version and a PostgreSQL version, some of this code will be implemented twice (once for each).<br />
<!--adsense#inside--><br />
Hopefully you are using database abstraction in your web applications, but for the purpose of this article weâ€™ll assume that you arenâ€™t.</p>
<h3>database.php</h3>
<p>[source:php]</p>
<p>&lt;?php<br />
function dbConnect()<br />
{<br />
$link = mysql_connect(&#8216;localhost&#8217;, &#8216;username&#8217;, &#8216;password&#8217;);<br />
if (!$link)<br />
return false;</p>
<p>return mysql_select_db(&#8216;phpriot&#8217;);<br />
}<br />
?&gt;</p>
<p>[/source]</p>
<h3>movies.php</h3>
<p>[source:php]</p>
<p>&lt;?php<br />
function getMovies()<br />
{<br />
$query = &#8217;select movie_id, title from movies order by ranking, lower(title)&#8217;;<br />
$result = mysql_query($query);</p>
<p>$movies = array();<br />
while ($row = mysql_fetch_object($result)) {<br />
$movies[$row-&gt;movie_id] = $row-&gt;title;<br />
}</p>
<p>return $movies;<br />
}<br />
?&gt;</p>
<p>[/source]</p>
<h3>index.php</h3>
<p>Hereâ€™s the main script that displays the list of movies. It is the same for both MySQL and PostgreSQL, as it will include the necessary code. At this point it is not styled and it is not yet possible to change the ordering. Weâ€™ll be adding each of those things in next.</p>
<p>[source:php]</p>
<p>&lt;?php<br />
require_once(&#8216;database.php&#8217;);<br />
require_once(&#8216;movies.php&#8217;);</p>
<p>if (!dbConnect()) {<br />
echo &#8216;Error connecting to database&#8217;;<br />
exit;<br />
}</p>
<p>$movies = getMovies();<br />
?&gt;<br />
&lt;!DOCTYPE html PUBLIC &#8220;-//W3C//DTD XHTML 1.0 Strict//EN&#8221; &#8220;DTD/xhtml1-strict.dtd&#8221;&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;phpRiot Sortable Lists&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;phpRiot Sortable Lists&lt;/h1&gt;</p>
<p>&lt;ul id=&#8221;movies_list&#8221;&gt;<br />
&lt;?php foreach ($movies as $movie_id =&gt; $title) { ?&gt;<br />
&lt;li&gt;&lt;?= $title ?&gt;&lt;/li&gt;<br />
&lt;?php } ?&gt;<br />
&lt;/ul&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p>
<p>[/source]</p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/05/12/creating-sortable-lists-with-php-and-ajax.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Interstitial pages with javascript</title>
		<link>http://9tutorials.com/2007/05/11/interstitial-pages-with-javascript.html</link>
		<comments>http://9tutorials.com/2007/05/11/interstitial-pages-with-javascript.html#comments</comments>
		<pubDate>Fri, 11 May 2007 15:59:16 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[advertising]]></category>
		<category><![CDATA[interstitial-page]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/05/11/interstitial-pages-with-javascript.html</guid>
		<description><![CDATA[When you click on a link, an interstitial page (usually an advert) appears  before you are shown the new page that you clicked to view. doInterstitial() is a simple javascript function that rewrites all links on a  page.

[source:javascript]
function doInterstitial(newURL, time) {
var links = document.getElementsByTagName(&#8216;a&#8217;);
for( var i=0; i&#60;links.length; i++) {
var oldURL = links[i].href;
links[i].href = [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p>When you click on a link, an interstitial page (usually an advert) appears  before you are shown the new page that you clicked to view. doInterstitial() is a simple javascript function that rewrites all links on a  page.</p>
<p><span id="more-38"></span><br />
[source:javascript]</p>
<p>function doInterstitial(newURL, time) {<br />
var links = document.getElementsByTagName(&#8216;a&#8217;);<br />
for( var i=0; i&lt;links.length; i++) {<br />
var oldURL = links[i].href;<br />
links[i].href = newURL + &#8216;?oldURL=&#8217; + oldURL + &#8216;&amp;timer=&#8217; + time;<br />
}<br />
}</p>
<p>[/source]</p>
<p>and can be called on page load (passing the advert url and a time  duration for the advert):</p>
<p>[source:html]<br />
&lt;body onload=&#8221;doInterstitial(&#8216;ad.html&#8217;, 2000);&#8221;&gt;</p>
<p>[/source]</p>
<p>The following demonstrates a simple interstitial advert. Its basic  function is to wait for a defined duration and then forward the user to their desired page.</p>
<p>The GET variables are parsed from the url, and used to find the duration and destination.</p>
<p>The biggest trick is the location.replace line which removes the advert from the history, stopping it appearing when the back button is pressed.</p>
<p>[source:html]</p>
<p>&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Ad&lt;/title&gt;<br />
&lt;script type=&#8221;text/javascript&#8221;&gt;<br />
function parseGetVars() {<br />
var args = new Array();<br />
var query = window.location.search.substring( 1 );<br />
if( query ) {<br />
var strList = query.split( &#8216;&amp;&#8217; );<br />
for( str in strList ) {<br />
var parts = strList[ str ].split( &#8216;=&#8217; );<br />
args[ unescape( parts[ 0 ] ) ] = unescape( parts[ 1 ] );<br />
}<br />
}<br />
return args;<br />
}<br />
function addEvent(obj, evType, fn, useCapture){<br />
if (obj.addEventListener){<br />
obj.addEventListener(evType, fn, useCapture);<br />
return true;<br />
} else if (obj.attachEvent){<br />
var r = obj.attachEvent(&#8220;on&#8221;+evType, fn);<br />
return r;<br />
} else {<br />
alert(&#8220;Handler could not be attached&#8221;);<br />
}<br />
}<br />
&lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;script type=&#8221;text/javascript&#8221;&gt;<br />
var get = parseGetVars();<br />
var oldURL = get['oldURL'];<br />
var timer = get['timer'];<br />
document.write(&#8216;&lt;p&gt;&lt;a href=&#8221;#&#8221; onclick=&#8221;window.location=\&#8221;+oldURL+&#8217;\';return(false);&#8221;&gt;Skip (will redirect to page in &#8216;+ parseInt(timer/1000) +&#8217; seconds).&lt;/a&gt;&lt;/p&gt;&#8217;);<br />
addEvent(window, &#8216;load&#8217;, function() {<br />
window.setTimeout(&#8216;location.replace(\&#8221;+oldURL+&#8217;\')&#8217;, get['timer'])<br />
}, false);<br />
&lt;/script&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p>
<p>[/source]</p>
<p><em>Copyright @ BrandNewBox 2007</em></p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/05/11/interstitial-pages-with-javascript.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
