Easy Ajax: A Case Study - Page 3

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.

The keepAlive function calls the PHP page I'll describe later. For now, understand that any URL could be used, and this one should return either "OK" or something else. The TMPL_VARs are again supplied by the publishing system to track the current editor and article.

The reason for the if is that even though Microsoft created XMLHttpRequest, Internet Explorer doesn't actually support the function without the use of ActiveX. Firefox, Safari, and other browsers do support it, however, and so we first check to see if one of those will work, and only fall back on the ActiveX approach if that fails. In either case, the idea is the same: we instantiate the object, tell the browser to call the processReqChange function if anything happens, and then open the URL and send it a null to get it moving.

This is where the asynchronous part comes in. The javascript function ends and the browser goes along its merry way. If the XMLHttpRequest object never returns, the browser is not hung up. When the object's state does change, the processReqChange function is fired off.

There are several different "ready states" the object can be in, so we first check to make sure we're in readyState 4, meaning we're finished. Given that, and given that we have control over the server side, the most likely result is that our status is 200, the standard HTTP status for "success." If that isn't the case, something unusual must be happening, so we'll display some error information and clear the interval so we don't keep hammering the server.

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

Article tags

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

Article Author: Phillip Winn

Phillip Winn is 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

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 Nov 21, 2009

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 October

top commenters Most prolific Commenters in 24 hrs