View Full Version : Tutorial: AJAX
gabrielcrowe
23-May-2007, 10:56 AM
AJAX is a javascript based technology, that lets you fetch pages, and use their data in your hosting page, without re-loading them.
In fact, its very much like server side includes, with respect to allowing external content to be included in your pages, but with the added bonus of allowing the included content to be changed when you want it to, and not when the server puts the page together on the server.
So, this is all a little complex, but what does it mean for actinic?
Firstly, it does not require any server side scripting, like php, or asp to make it work. its all client side javascript.
This is also not very good for SEO, since most search engines have no idea what javascript included content looks like and wont see it in your final pages.
For example, you could take the information from the popup, normally associated with a product, and add it inline to the page, when the user clicks a button. And then hide it again, when they click it again. In this example, i'm going to simply include some text.
Here is a javascript function, that we need to trigger an AJAX call.
function ajaxGetPage(pageUrl,divID)
{
var d = new Date();
var time = d.getTime();
pageUrl=pageUrl+'&t='+time;
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
//alert (xmlHttp.responseText);
if(divID=="dispcontent") {
document.getElementById(divID).innerHTML = xmlHttp.responseText;
}
else {
if(document.getElementById(divID).innerHTML=="") {
document.getElementById(divID).innerHTML = xmlHttp.responseText;
}
else {
document.getElementById(divID).innerHTML = "";
}
}
}
}
xmlHttp.open("GET",pageUrl,true);
xmlHttp.send(null);
}
looks complex, doesnt it?
Its actually more complex than it should be, but only becasue i'v added some clever browser compatibility i found floating around on the web.
notice this part, right at the top of the code: ajaxGetPage(pageUrl,divID)
'ajaxGetPage' is the name of the function. We'll use this later on, to call this block of code.
'pageurl' is the name of the page we want to load date from. This is going to be simple text in this example.
'divID' is the name of the container, that we will insert the code from our fetched page into.
It works like this:
1: the user clicks the link triggering the function above
2: the function uses AJAX to fetch the page we want to fetch.
3: the function inserts the data into the host page without re-loading.
4: the loaded content assumes any styles and colours that the parent page has.
5: the user clicks on the link again, and the data in the container is removed.
Next we need a container to keep our data in once we load it.
<div id="mytarget"></div>
Remember earlier, the 'divID' variable in our function? if the name 'mytarget' is passed into it, then, it will tell our function to load the data into that container. easy huh?
Now, we need to make a link. This link will have some javascript, that causes the function to trigger.
In this simple example, mine looks like this:
<a href="#" onclick="ajaxGetPage('include.htm','mytarget')">click here to toggle</a>
but wait? whats this 'include.htm' you can see in the link? well, thats the data we'll be including. it can be anything you want. it could just as easily be one of your info_000.html files that actinic generates.
Remember, that the included page will be included in your sites html structure. so remember to remove all the body, html and head tagging. sicne the parent page allready has those!
Included is a very simple working example. 2 files. 'parent.html' and 'child.html'. put these in the same folder and load 'parent.html' to see ajax in action.
REMEMBER: THIS IS A SERVER TECHNOLOGY ONLY. it wont work on your desktop. you'll need to upload it to an actual webserver.
hope this has been any help to anyone learning AJAX.
TraceyHand
23-May-2007, 11:21 AM
and I thought Ajax was a cleaning fluid
:p
now *that* I'd understand!
gabrielcrowe
23-May-2007, 11:36 AM
*someone* had to say it.
:D
pinbrook
23-May-2007, 11:37 AM
Gabe
Wow....
have you got an example of where you've used this on an actinic site?
TraceyHand
23-May-2007, 11:41 AM
*someone* had to say it.
yup
and this dim one took the bait
;)
gabrielcrowe
23-May-2007, 11:46 AM
Gabe
Wow....
have you got an example of where you've used this on an actinic site?
well, its very easy to make it work with V8
drop the code into your templates, but instead of calling the 'child.html' call the info_000.html file of the popup page.
remember to edit the template of the popup so that no head body or html tags exist.
they will break your outer template, ending the html too soon.
i dont know the variable to do it but:
<a href="#" onclick="ajaxGetPage('THIS_IS_THE_URL_OF_THE_POPUP','mytarget')">click here to toggle</a>
oh and i almost forgot... i'v never used this on an actinic store. its just one of the many emerging web technologies that are taking over.
http://script.aculo.us/ for example, and the prototype framework.
this type of code features heavily in our Actinet Product. its important to keep the accesses to the database to nibbles, instead of big honking bites. speed is of the issue, and AJAx allows us to break a large page of multiple database acceses, down to several smaller ones, increasing overall page load responsiveness.
see, the AJAX is executed *after* the page renders, so that your requested data is loaded in the background.
fergusw
23-May-2007, 03:18 PM
Excellent AJAX scripting there Gabriel.
You can see it in Actinic working here:
http://www.teclan.com/AJAX/acatalog/Books.html
[Note: Its only on the first product and I left a normal ext info lin above the AJAX one for comparison]
A very simply example, but it shows the difference between the normal ext info link and the AJAX version. Very smooth.
There are many exciting new UI based effects and tricks that can be achieved with AJAX.
Note:
I had to tweak the AJAX scripting a little to get it working. I had to change
function ajaxGetPage(pageUrl,divID)
// {
var d = new Date();
var time = d.getTime();
pageUrl=pageUrl+'&t='+time;
to
function ajaxGetPage(pageUrl,divID)
{
var d = new Date();
var time = d.getTime();
//pageUrl=pageUrl+'&t='+time;
i.e. remove the & from the querystring on the request. Not dug too deeply to check why though :)
Very interesting though.... and well worth more experimenting. I have already seen a few sites making excellent use of AJAX in Actinic.
gabrielcrowe
23-May-2007, 04:00 PM
beautiful example there.
thanks for showing people the actinic integration.
the reason 'time' is added to thie querystring is actually important.
IE6 and IE7 use some agressive cache, that caches all the ajax. Essentially, if you update the inserted code, IE fails to use the new version, favouring the cache version every time.
adding the time causes the page it calles to be 'different'.
but if the example still works without it, then thats good :)
use this:
function ajaxGetPage(pageUrl,divID)
{
var d = new Date();
var time = d.getTime();
pageUrl=pageUrl+'?t='+time;
usually, this function adds time on to the string as the last item, assuming you've allready added some query strings uwing the normal syntax:
page.htm?item1=this&iitem2=that&t=[time]
swapping the ampersand for a questionmarks, works fine.
pinbrook
23-May-2007, 06:13 PM
You can see it in Actinic working here:
http://www.teclan.com/AJAX/acatalog/Books.html
[Note: Its only on the first product and I left a normal ext info lin above the AJAX one for comparison]
aaaahhhh Neat!
Paul Bulpit
23-May-2007, 09:01 PM
I think I have a use for this - please could one of you experts start a thread giving step by step instructions?
Gabriel invented it, Fergus applied it & Jo is impressed - now can the rank & file put it into use?
fergusw
23-May-2007, 10:22 PM
please could one of you experts start a thread giving step by step instructions?
Hmmm ..... here goes. This is how I implemented it. (for V8)
Step 1: [Insert the Ajax script into Actinic]
Open actiniccore.js (found in site1 directory) in notepad and add the following to the very end
/***********************************************************
*
* AJAX Script
*
************************************************************/
function ajaxGetPage(pageUrl,divID)
{
var d = new Date();
var time = d.getTime();
pageUrl=pageUrl+'?t='+time;
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
//alert (xmlHttp.responseText);
if(divID=="dispcontent") {
document.getElementById(divID).innerHTML = xmlHttp.responseText;
}
else {
if(document.getElementById(divID).innerHTML=="") {
document.getElementById(divID).innerHTML = xmlHttp.responseText;
}
else {
document.getElementById(divID).innerHTML = "";
}
}
}
}
xmlHttp.open("GET",pageUrl,true);
xmlHttp.send(null);
}
Step 2: [amend the Extended info layout]
First I copied an existing Extended info layout from the library from "design | Library" and renamed it something like "AjaxExtendedInfo". I then amended the HTML code in the layout to remove eveything except
<Actinic:Variable Name="ExtendedInfoText"/>
Step 3: [Choose the ext info layout]
I made sure that I selected the newly created extended info layout for the product I wanted this to work for. Making sure, also, that it has extended info page ticked etc....
Step 4: [Adding the <div> on product layout where you want the ext info text to appear]
I amended a product layout and added
<div id="mytarget"></div>
to the layout where I wanted the additional ext info text to appear. In my example I placed it just before the "ProductAlsoBought" layout.
Step 5: [Adding the AJAX function]
Finally I amended the product layout (via the design tab again) to change
<a href="javascript:ShowPopUp('<actinic:variable name="ExtendedInfoPageEncoded" />',<actinic:variable name="ExtInfoWindowWidth" />,<actinic:variable name="ExtInfoWindowHeight" />);">
<actinic:variable name="ExtendedInfoLinkText" />
</a>
to
<a href="#" onclick="ajaxGetPage('<actinic:variable name="CatalogURL" /><actinic:variable name="ExtendedInfoPageName" />','mytarget')"><actinic:variable name="ExtendedInfoLinkText" /></a>
I then made sure that I had Display by "text link" for the extended info page selected.
I uploaded and, hey presto!
It is not 100% fully tested, however this should be enough to get you under way.
gabrielcrowe
24-May-2007, 08:26 AM
oop! you forgot to add the love
*sprinkles love on the whole kit and kaboodle*
cdicken
24-May-2007, 09:18 AM
Excellent stuff.
We use a little bit of a AJAX currently in v8 to handle the advanced best sellers and new products lists (i.e. the lists are generated as separate pages and then included in the site pages on the server). This is in the Advanced User Guide.
gabrielcrowe
24-May-2007, 09:25 AM
is the ajax in actinic re-usable? to save writing code that does not need to be written?
fergusw
24-May-2007, 09:44 AM
Chris,
I take it you are referring to the
function HtmlInclude()
in the actiniccore.js file?
Could this not be adapted to accept parameters and allow this usage as well?
FYI : I've been playing a little with different extended info layouts on this example
http://www.teclan.com/AJAX/acatalog/Books.html (http://www.teclan.com/AJAX/acatalog/Books.html#)
With a little styling and a tweak to the AJAX java code (to hide/unhide the DIV element) you can control individual layouts for each product.
gabrielcrowe
24-May-2007, 10:13 AM
just a little note about my original code, this section.
//alert (xmlHttp.responseText);
if(divID=="dispcontent") {
document.getElementById(divID).innerHTML = xmlHttp.responseText;
}
else {
if(document.getElementById(divID).innerHTML=="") {
document.getElementById(divID).innerHTML = xmlHttp.responseText;
}
else {
document.getElementById(divID).innerHTML = "";
}
isnt really required.
this code was pulled from something else. it can easily be trimmed to:
if(document.getElementById(divID).innerHTML=="") {
document.getElementById(divID).innerHTML = xmlHttp.responseText;
}
else {
document.getElementById(divID).innerHTML = "";
}
and if you dont want it to toggle, that is to say, a simple link that makes the content stay, you'll just do this:
document.getElementById(divID).innerHTML = xmlHttp.responseText;
;)
*and*
for those that want the item to automatically populate, add trigger the code just after your container DIV.
<script>ajaxGetPage('<actinic:variable name="CatalogURL" /><actinic:variable name="ExtendedInfoPageName" />','mytarget')</script>
fergusw
24-May-2007, 10:18 AM
and if you want to have the target div hidden (e.g. so bgcolors and borders dont show when it's empty) then could you not use
if(document.getElementById(divID).innerHTML=="") {
document.getElementById(divID).innerHTML = xmlHttp.responseText;
document.getElementById(divID).style.visibility = 'visible';
}
else {
document.getElementById(divID).innerHTML = "";
document.getElementById(divID).style.visibility = 'hidden';
}
and set the original div to something like
<div id="mytarget" style="visibility:hidden;"></div>
gabrielcrowe
24-May-2007, 10:20 AM
well now, isnt actinic looking more web 2.0 by the moment!
completerookie
24-May-2007, 11:00 AM
well now, isnt actinic looking more web 2.0 by the moment!
I'd love the latest version to look more like Web 1.0 at the moment
Paul Bulpit
24-May-2007, 09:53 PM
I've just come back because I intend to use this - to find you've all gone beserk!
Up to Fergus's step by step, fair enough. Thanks, F, for you prompt response last night, I intend to implement this on a live site this week-end.
But the enhancements that follow will need considerable refinement I fear.
completerookie
25-May-2007, 05:59 AM
But the enhancements that follow will need considerable refinement I fear.
Its just a starting point, but in keeping with Actinic, most enhancements need tweeking
ianb
13-Jun-2007, 12:06 PM
Sorry folks, completely lost. I'm sure this is very easy but I'm only a simple sailor.
I've lost track of where I stick all this stuff, in templates, .js code, fragments, ugh.
Am I right in thinking that I need to:
1. add some code to a .js files somewhere
2. create a new template
3. add into that template a call to the code I want displayed, e.g. http://www.citnexus.co.uk/ourfaqs/index.html
Then this code will display within the Actinic page without reloading AND when links in this code are clicked they will also remain on the same page?
Or am I being stupid as usual!
acompton
18-Feb-2008, 08:24 PM
Many thank to Gabriel for this exellent post.
Using Ajax I've been able to incorporate some PHP I've been working on and produce this page:
www.greenknightgames.co.uk/acatalog/All_Games.html
(I know the lists aren't in alpha order - I'm working on that)
pnagames
19-Feb-2008, 09:50 AM
Right
Quick Q
had a look on the example from Alan
what we want to bring back is a customvar on the products. can the split be done based on that?
also can it bring back the main image of the product?
jxm28788
19-Feb-2008, 09:51 AM
You can see it in Actinic working here:
http://www.teclan.com/AJAX/acatalog/Books.html
Did you remove it? When I go to that page I don't see anything... no products listed in that section.
fergusw
19-Feb-2008, 09:55 AM
We took it down temporarily - I'll see if I can throw it back up for you :)
TraceyHand
19-Feb-2008, 09:57 AM
I'll see if I can throw it back up for you :)
last night must have been a good(bad) night?
Darren B
19-Feb-2008, 10:13 AM
Hmmmmmm Good Bad Night, depends on the way you see things ;)
jxm28788
19-Feb-2008, 10:18 AM
We took it down temporarily - I'll see if I can throw it back up for you :)
Hey, that is kind of cool. Not sure I'd want it on my product pages, but it is supposed to be pretty useful when used during the checkout procedure - has anyone done that yet? Maybe something for v9 checkout...?
fergusw
19-Feb-2008, 11:36 AM
OK - back up now :)
acompton
19-Feb-2008, 11:37 AM
what we want to bring back is a customvar on the products. can the split be done based on that?
In effect, create your product sections this way - I wondered about that.
also can it bring back the main image of the product?
Mine is driven off the Froogle feed data, which I post-process with a command-line program. Any data that is in that feed (or a Mole-End feed) can theoretically be presented in this way.
I see it as an alternative customer search. Some people have a product title in mind and will use the list instead of the search, and something else might catch their eye. Also, the hobby gamers visit the site with a list of titles they've got their eye on. Its an easy way for them to browse the complete product list - and possibly spot something they hadn't thought of.
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.