Tidbits @ Kassemi

A collection of opinions, thoughts, tricks and misc. information.

Sunday, September 18, 2005

 

Re-think your AJAX overuse

Okay. I know I've been posting loads of AJAX stuff recently, but after taking a look at how a few other sites are implementing this technology, I decided I need a warning: NOT ALL USERS BROWSE WITH JAVASCRIPT!

AJAX programming is very well supported by the major browsers, but only if AJAX is enabled. Some stats point out that up to 30% of users browse the internet with javascript disabled. You need to be sure that you allow some method of viewing your page without javascript. So today, a quick discussion on the PHP DOM, which will probably throw out much of your JS DOM usage...

The Javascript DOM makes it very easy to add information to each page after it is loaded, and this is exactly what most of the AJAX programs on the internet are doing these days (my div browser below is a good example of that). The best use of AJAX can also be the hardest to find a very quick substitute for. Take for instance the star feature on gmail. A click on the star sends a message through the request object to the gmail server, telling the server that the message should now be starred. No response is needed (although one may be given), and the client side script applies a star to the message. The best way to program something like this is definitely to program the non-AJAX version first, and then to add the new functionality for a browser that supports it.

Anyway, the PHP DOM class: http://www.php.net/manual/en/ref.dom.php

The basic usage is as follows:


$dom = new DOMDocument;
/* For HTML */
$dom->loadHTML($html);
/* For XML */
$dom->loadXML($xml);
$links = $dom->getElementsByTagName('a');
foreach($links as $link){
/* Looping through each link in the provided HTML, get the location the link points to: */
$location = $link->getAttribute('href');
if($location == ''){
/* There was no href attribute, or it is empty. the DOM will not return false for this
method, if there was no location specified, send to the index (relative)*/
$link->setAttribute('href', 'index.php');
}else{
/* We are going to send the user through the safe_redirect script to avoid sharing
the session id */
$link->setAttribute('href', 'safe_out.php?loc='.$link);
}
}



Read the documentation for this class, and you'll see it works in exactly the same way as the JS DOM. The example above changed all links on the page to move through the safe_out.php script, which could redirect a user to a page after stripping the phpsessid variable from the $_GET array, lessening the probablity for a Cross-site scripting attack...

Again, I don't have much to write on today, as I haven't really had time to do anything interesting. No cool new features, just fixing bugs in code...

Don't worry though, you non-existent audience (save you, of course mav3n), I'll soon start putting up some more interesting stuff.

-James

Comments: Post a Comment



<< Home

Archives

August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006  

This page is powered by Blogger. Isn't yours?