Easy Ajax: A Case Study - Page 2

I used a bit of javascript and a separate internal web page written in PHP. That's it. In this case, I didn't even use any XML.

Javascript

var req;
function initPage() {
  kaTimer=setInterval(keepAlive, 1000);
}
function keepAlive() {
  var url = "/mt/keepalive.php?aid=<TMPL_VAR _
                  NAME=AUTHOR_ID>&eid=<TMPL_VAR NAME=ID>";
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.send(null);
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if(req){
      req.onreadystatechange = processReqChange;
      req.open("GET", url, true);
      req.send();
    }
  }
}
function processReqChange() {
  if(req.readyState==4){
    if(req.status==200){
      if(req.responseText=='OK'){
        document.getElementById('saveButton').style.visibility='visible';
        document.getElementById('ajaxConsole').innerHTML='';
        clearInterval(kaTimer);
        kaTimer=setInterval(keepAlive, 30000);
      }else{
        document.getElementById('ajaxConsole').innerHTML = 'LOCKED: ' + _
                  req.responseText;
        if(document.getElementById('ajaxConsole').innerHTML.substr(0,8) _
                  == 'LOCKED: ') {
          clearInterval(kaTimer);
        }
      }
    }else{
      alert('Problem: [' + req.status + '] ' + req.statusText);
      clearInterval(kaTimer);
    }
    req.onreadystatechange = new function(){};
  }
}
function activateEditing() {
  document.getElementById('saveButton').style.visibility='visible';
  document.getElementById('ajaxConsole').innerHTML='';
  clearInterval(kaTimer);
  kaTimer=setInterval(keepAlive, 30000);
}
window.onload=initPage();

With this code, I've set up three functions: initPage(), keepAlive(), and processReqChange(). Your own Ajax code will need functions very much like these. The window.onload=initPage(); starts the ball rolling.

First, let me paint a little picture of what the rest of this page looks like. It's an article-editing screen, with buttons to Save, Preview, and Publish. The ID for the Save button is "saveButton". Near the top of the screen I've added a div that looks like this:

<div id="ajaxConsole"><TMPL_UNLESS NAME="STATUS_DRAFT">Do not edit this article! Wait until this text either disappears or is replaced by a "locked" message.<br/>It shouldn't take more than a couple of seconds. If you do not have Javascript enabled, you will not be able to edit an article in 'Pending' or 'Publish' status.</TMPL_UNLESS></div>

The TMPL tags are interpreted by the publishing system so that this div doesn't exist when an article has never been saved. Clearly there can be only one author then!

When the page is first loading, that text will be visible. Once the window.onload event is triggered, however, the initPage() function should be called, which tells the browser to call the keepAlive function once per second.

Warning! The window.onload can be triggered before the page has actually finished loading, which can cause... oddness. It is for this reason that I set the function to be called at frequent intervals, so it will seem to complete as soon as the page has completed loading, rather than waiting until the next interval comes around.

Continued on the next page Page 1 — Page 2 — Page 3Page 4Page 5

Article tags

Spread the word
Bookmark and Share
Profile image for phillip-winn

Article Author: Phillip Winn

Phillip Winn was the Chief Geek for Blogcritics, and a blogger since 1995. He may currently be found and followed as @pwinn on Twitter.

Visit Phillip Winn's author pagePhillip Winn's Blog

Read comments on this article, and add some feedback of your own
  • No image found
  • No image found

Article comments

  • 1 - Aaman

    Dec 31, 2005 at 12:28 am

    I wish that Ajax book was cheaper:)

    Incidentally, is this true AJAX, does the reliance on PHP and SQL not mean a new paradigm, not reliant on XML?

  • 2 - Phillip Winn

    Dec 31, 2005 at 11:49 am

    It's still XMLHttpRequest, so all three elements of AJAX are present. The PHP and Mysql are, I think, incidental. It could as easily be anything else, or an external webservice, on the "back end."

Add your comment, speak your mind

Personal attacks are NOT allowed.
Please read our comment policy.
Please preview your comment.

blogcritics lists for Feb 13, 2012

fresh articles Most recent articles site-wide

fresh comments Most recent comments site-wide

most comments Most comments in 24hrs

top writers Most prolific Blogcritics for January

top commenters Most prolific Commenters in 24 hrs