Archive for January, 2007

phplondon conference 2007

Thursday, January 18th, 2007

A couple years I got to spend some time going to the phplondon user group. Its was a great group full of knowledgeable PHP developers who love to have a good time. They have been successful at growing the group over the last couple years and just let me know that they are having there second conference. The conference is a one day event, and from what I read about last years should be a great event.

CONFERENCE DETAILS:
phplondon conference 2007
23rd February 2007 @ The Keyworth Centre, London

After the success of the 2006 php conference, phplondon is organising a second conference for anyone who has an interest in all things php. You will be able to listen to an exciting mix of topics from these diverse and thought provoking speakers.

- Cal Evens (Zend) - My First Mashup
- Simon Laws (IBM) - Web services - drop it into Apache and away you go!
- Kevlin Henney - Objects of Desire
- Rasmus Lerdorf (Yahoo!) - Fast and Rich Web Applications with PHP 5
- William (Bill) Gaver (Goldsmiths University) - Ludic Interfaces

You will also have the opportunity to network with others and share experiences both at the conference itself and chat with the speakers informally at a special event afterwards.

Early bird price is £50, or £75 on the day. To find out more and book online visit us at http://www.phpconference.co.uk

Webthumb Contest is Closed

Tuesday, January 16th, 2007

The WebThumb contest is now closed, the winners will be announced in the next couple days.

jQuery Image Strip

Thursday, January 11th, 2007

Working for Obu Web i’ve found myself spending less time on code I would say meets the definition of AJAX I put in my book and more time on code that used to be called DHTML. But I guess I’m ok calling it AJAX, at least its pronouncable and building little JavaScript widgets is fun.

Today’s widget is an Image Strip, it has a lot of real uses, but the way I like too use it is for fancy visual effects.

(more…)

Compressing JavaScript and CSS

Wednesday, January 10th, 2007

When your building fancy AJAX websites one thing that tends to happen is you end up loading amounts of JavaScript and CSS on your pages. And while browsers are smart and do a lot of client side caching you can’t get rid of that weight on your first page load.

For example on my blog I have about 60K of JavaScript and 10K of CSS. Now this isn’t horrible but when you figure images and 90K of HTML it doesn’t take long to get to my 200K total page weight.

There are a lot of various approaches for cutting down the size of JavaScript and CSS. Some of the common ones are removing whitespace and comments for JS and CSS, or using scripts which use alternate smaller syntaxes. There are a couple problems with this approach first it just doesn’t save as much space as I’d like, you can cut 60K of JavaScript down to 30K but not 10K. Second it makes debugging horrible, every JavaScript or CSS error comes from line 1 or the file.

There is a better approach and it comes from a technology in our browsers called Content Encoding. With the right headers we can send gzip’d content to the browser and it will automatically uncompress it. All modern browsers support this so its a huge win.

So now we have figure out how to compress our content, the simplest option for static files is Apache’s mod_deflate since chances are its already installed and you can enable it with a couple minutes of poking around in your httpd conf file. But it does have the disadvantage of recompressing the files for each request so it uses up some extra cpu.

mod_gzip has a similar feature set to mod_deflate with the addition of the ability to cache the compressed files but what I really want is a solution that can run on any run of the mill apache server.

To make this happen we need to compress a JavaScript file by hand and then make Apache serve it up with the correct headers.
The headers are pretty simple, Content-Type should be text/javascript and Content-Encoding should be gzip

To set the needed headers you just need to add the following rule to apache, it can go in your httpd.conf or in a .htaccess file.

<files *.js.gz>
ForceType text/javascript
Header set Content-Encoding: gzip
</files>

You can see this in action at with a small sample I made.

Now im sure your next question is what browsers support this, from my limited testing that FireFox 1.5+ and IE 6+ both work fine. From what i’ve ready anything back to version 4 of most browsers should work.

The only only downside to this approach is you have to make sure the keep your gzipped versions of files up to date and you have to update all your code to refer to the gzipped version. I don’t have a solution to the first problem, though running gzip file.js -c > file.js.gz is handy if you want to have both the uncompressed and compressed version of the file hanging around.

I thought I had a solution for the second problem using mod_rewrite. However I haven’t been able to get it working right so if you have any tips here’s my current attempt. The conditions seem to work right, but once the rediret happens the force type and content encoding header don’t seem to get set anymore.

<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_ACCEPT} >gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.+).js$ $1.js.gz [QSA,L]
</ifmodule>

Note: For testing that things are working correctly you’ll want a tool to view the headers of the http response. If you want a web based tool web-sniffer seems to work well.

WebThumb content ends on the 15th

Tuesday, January 9th, 2007

The WebThumb content ends on the 15th. I know there are couple projects out there that haven’t officially entered. If you want to have a chance to win an upgraded Webthumb account make sure to get your entries in, the contest website has all the details.

Also don’t forgot about the EasyThumb api, its a very simple to use API and would be great for Wordpress search plus thumbnails or thumbnails for comment urls.

Deciding What AJAX Programming Style to Use

Thursday, January 4th, 2007

In my AJAX book, Understanding AJAX, I spend a lot of time talking about the various programming styles you can use with AJAX. Looking back at the chapter it seems to me there are 2 major styles of programming and large number of varitions depending on what data encodings were using, and where the bulk of our programming actually happens.

Looking at my own day to day development I tend to fall into a couple main approaches.

  • AJAX page chunks (sending standard GET/POST requests, and updating part of the page by innerHTMLing in the respose)
  • RPC using generated proxies
  • Widgeted AJAX using RPC in the background

Now each of these approaches has its advantages and disadvantages. AJAX page chunks are easy to understand, on the PHP side im generating HTML using a standard MVC framework, and I can just turn off my outer wrapper for the chunks. The disadvantage is that my JavaScript code doesn’t have a lot of power in this approach. I’m skipping full page reloads but I still might be updating a large percentage of the page for a very small change. Its also hard to update disperate parts of a page. To solve this I sometimes find myself returning an array of HTML chunks but this quickly leads to both the PHP and JavaScript sides of the equation getting overly complicated and brittle.

AJAX Page chunks are especially nice when dealing with forms, you just serialize the form to a query string, POST it to the PHP side and return the updated form, replacing the innerHTML of the forms parent with an updated form. I consider page chunks to be my first option, they make it easy to use a single code base to support AJAX and non-AJAX operation, and most importantly are an easy concept for new developers to pick up. It also leverages current skills and keeps the amount of JavaScript down since devlopers are more likely to look towards PHP (or whatever backend lanuage you use) to solve there problems.

RPC style AJAX using generated proxies is very powerful while still being pretty simple. HTML_AJAX makes programming in this style extremly easy to implement. On the PHP side I write a class that becomes the external API to my application. HTML_AJAX covers exporting the API and encoding the data, and on the JavaScript I get a generate proxy that makes that API easy to work with.

Then its time to write a lot of JavaScript code. The biggest benefit of any RPC approach is you have data in your JavaScript not just some HTML. I tend to use an approach like this when i’m building a JavaScript application. It tends towards a more client server model and moves things away from a standard MVC approach. The biggest disadvantage to this approach is the complexity. You end up with huge amounts of JavaScript code, and much smaller amounts of PHP. This wouldn’t be horrible if JavaScript was mature and consistent on all browsers but its not. Plus you can’t trust the client so the PHP code still ends up being larger then you’d like since it has too enforce all the security and integrity rules of the application. But at the end of the day sometimes you need the power.

The last style I use is related to the more standard RPC approaches but I’ll often use it along with AJAX page chunks. With widgeted AJAX I develop a self contained set of HTML and JavaScript code that can be easily integrated into any page. This might be a autocomplete/suggest style dropdown or a live grid. I normally use RPC on the backend inside these widgets since they need actual data but what there using doesn’t really matter. The point of the widget is too make things self contained enough that they can be easily integrated with anything.

Looking at these styles I think each can have it place, but I think my decisions on what to use are more and more driven by simplicity. When looking at a problem whats the simplest solution. The more I use AJAX the more I see that its added complexity is its biggest problem. Like any other programming the simplest solution is usually the best one.

I’d like to hear your thoughts on the matter, what AJAX programming styles do you use and why?

This circle expands additional navigation
You should really buy my AJAX book.
You'll learn not only the technology you need for implementation but also an understanding that will help you get the most from AJAX.
You'll also have my eternal thanks :-)