View Full Version : How to: Create a AJAX site!


Andrew Green
02-24-2006, 11:04 PM
Last couple days I've been playing with AJAX a little, here's one of my experiments, with comments added.

A simple, commented, piece of code to create a AJAX based site.

You will need to point the links to somewhere, but to see what is going on you can use this:

loadme.php
<?php

$x = $_GET['action'];
echo "$x";
?>

This method would create much faster loading pages, the downside would be you break normal page navigation. Th back button won't work, refresh will take you back to the first page and you can't bookmark anything but the main page... So I wouldn't really reccomend doing a site like this, but the idea on how to do more useful things is there.

<html>
<head>
<script language="javascript">

function loadpage(page)
{
try
{
// Mozilla uses one way, IE another...
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");
}

catch (e)
{
// Error = Ajax isn't supported, load the page some other way.
// Either a incompatible browser or IE with active x off.
}

xmlhttp.onreadystatechange = trigger;
// If the state changes, call the "trigger" function

xmlhttp.open("GET", page);
// Open the page passed to the function

xmlhttp.send(null);
// If this was a post request it would not be null

}

function trigger()
{
// If it's ready and everything is ok, load the response text into the pagecontent section
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
{
document.getElementById("pagecontent").innerHTML = xmlhttp.responseText;
}
}

</script>

</head>

<body onload="loadurl('loadme.php?action=1')">
<!-- Load the default page right away -->
<table style='width:100%' border='1'>
<tr>
<td colspan='4' style='background-color:black;color:white;tex-align:center;font-size:200%;'>Title Banner</td>
</tr>
<tr>
<td style='cursor:pointer;' onmouseover="this.style.backgroundColor='#ffffd0'" onmouseout="this.style.backgroundColor='#ffffff'" onClick="loadpage('loadme.php?action=1')">
<a class="nav" href="loadme.php?action=1" onClick="return false">link 1</a>
<!-- If javascript is inactive onclick links do nothing
This gives you a back up plan, of course you want it to link to a page
that includes the template and not just the content...
Also will come in on middle clicks in firefox ;)
onclick="return false" will kill the link if javascript is present,
otherwise it processes the javascript link, and the <a> link-->
</td>
<td style='cursor:pointer;' onmouseover="this.style.backgroundColor='#ffffd0'" onmouseout="this.style.backgroundColor='#ffffff'" onClick="loadpage('loadme.php?action=2')">
<a class="nav" href="loadme.php?action=2" onClick="return false">link 2</a>
</td>
<td style='cursor:pointer;' onmouseover="this.style.backgroundColor='#ffffd0'" onmouseout="this.style.backgroundColor='#ffffff'" onClick="loadpage('loadme.php?action=3')">
<a class="nav" href="loadme.php?action=3" onClick="return false">link 3</a>
</td>
<td style='cursor:pointer;' onmouseover="this.style.backgroundColor='#ffffd0'" onmouseout="this.style.backgroundColor='#ffffff'" onClick="loadpage('loadme.php?action=4')">
<a class="nav" href="loadme.php?action=4" onClick="return false">link 4</a>
</td>
</tr>
<tr>
<td colspan='4' id="pagecontent" >Page contents will go here</td></td>
</tr>
</table>
</body>
</html>

Andrew Green
07-30-2006, 02:17 AM
Here's a little something I ran across....

IE has a nasty habit of sticking stuff in the cache when you don't want it too, so if you find only the initial screen is loading, then just the same stuff everytime, that's probably the problem :)

This happens when you request the page using GET, one solution obviously is don't use GET, use POST. Another is simply ad a timestamp to the GET request "&time=....."

There is also a solution involving setting a no-cache header which is more technically correct, but I've seen a few complaints about it not working...

BlueDragon1981
01-17-2007, 01:23 AM
So what info can you give me on AJAX. Do you use it? Do you have some good resources? How difficult is the coding?

It is the way a lot of things are going to now so I'm wondering if it is worth looking into.

Andrew Green
01-17-2007, 11:30 AM
It's nothing terribly new, just old stuff applied in a new way.

At a simple level it i just using javascript to call some other script and insert new things into a page that is already being displayed.

AJAX is used a fair bit in vbulletin, the lookup on usernames when sending PM's, the quick reply, message editing, etc.

BlueDragon1981
01-17-2007, 10:41 PM
I recall reading that Google uses it for its new Google Doc & Spreadsheet app.... also read about the internet os sites using it. (can't remember their names now that I am trying to.)

Andrew Green
01-18-2007, 07:47 PM
Makes sense, I remember thinking some time ago, "Hey, this Ajax stuff could make a web based spread sheet" Then before I had the time and will to create such a thing Google did. So now I will remain bitter at them for years as I coulda been rich ;)

BlueDragon1981
01-24-2007, 11:06 PM
lol.... yeah google takes the fun out of thing...lol..