Archive for February, 2007

YUI 2.2.0 Released

YAHOO have just released the 2.2.0 version of their famous YUI library. In actual fact, version 2.2.0 is really 0.12.2

They’ve changed their versioning, which will be much easier to follow. They’ve also included mega amount of extra features to their package. It’s far too much to mention, you’ll have to check it out for yourself.

No Comments »

Suckerfish HoverLightbox Redux

A new version of the Suckerfish HoverLightbox has been released.

Check it out here.

No Comments »

How to use Gzip to load your site faster

Lately I’ve been reading various random articles about some browsers ability to deflate zipped webpages, so that there is less bandwidth and faster loading times for websites. Although this ability has been around for a while, it’s only been lately that I have a need to use it.

To understand the concept of page compression and the support for it, visit this site. But in short, most browsers support it, including IE6 and 7 (according to this test page).

For those rare browsers that do not support compression, they just receive a non-compressed page. So effectively, you’d be nuts not to use compression on your site. There’s nothing to lose (apart from a slight server load increase, but I’m told Slashdot uses compression even with it’s large amount of site hits).

Here is a real world example of a page compressed with Gzip:

Gzip in action

Here I’ve compressed the html document, css and javascript files. Look at the total, that page is 75% smaller, and 75% faster to load in your browser.

This tutorial will only examine one particular way of implementing Gzip compression with PHP, but there is another way to accomplish it with Apache if you have administration access to your server.

How to implement Gzip compression in 5 minutes

Step 1: Enable zlib compression in php. You can do this many ways, just choose one:

  • Add a line to your .htaccess file:
    php_flag zlib.output_compression On
  • Add or change a line in your php.ini file:
    zlib.output_compression = On
  • Add a line to the top of your PHP scripts before any output:
    ini_set(‘zlib_output_compression’,'On’);

Now that you’ve done that (and particularly with the first 2 options), any file that is passed through PHP will be Gzipped if the browser supports it.

Step 2: If you want to compress other files, javascript, css, etc:

If your file does not pass through PHP before it is outputted, you can change the extension of the file to .php and it will be compressed. For this example I’ll use core.js.

  1. Rename core.js to something like core.js.php
  2. Open core.js.php in your favourite text editor and add this to the beggining of the file (before anything else) then save:
    <?php header("Content-type: text/js"); ?>

    Download this code: gzip_js_example.php

  3. Reference core.js.php in your html just as you would normally, the browser will see it as a JS file, not PHP:
    <script language="javascript" src="core.js.php" type="text/javascript"></script>

    Download this code: gzip_js_example.html

So to explain, we rename the file to make it a php file, so it is passed through the PHP compiler. Then we add a mime header line to the file, so that the browser knows what type of file it’s meant to be. Then we reference the file (if it’s a CSS or JS file for example) just as we normally would.

Remember to change the mime type to text/css for CSS files.

A quick note with this method. Only compress files that are specifically meant for a web browser (html, js, css, and xml). If you try this with a PDF file, for example, it works in browsers, but not when saved and displayed in Adobe PDF Reader.
If you’re finding that this method causes errors in your scripts, chances are that you haven’t got the zlib module installed with PHP.

Technorati: php, gzip, compression, browsers

No Comments »

YUI Unobstrusive Javascript Validation

Ever since I started creating web forms, I have been implementing some sort of javascript validation. This often involved a fair bit of javascript attached to the onsubmit events of the forms. I was even using a Dreamweaver extension that did this for me!

I’ve since been shown the light, and tried some other javascript validation. One package gave me the idea for creating a method of providing validation using Class names on form elements, no messy javascript. Another package showed me more in depth how to accomplish it. But it was based on the Prototype library, and I would have to include that to my scripts (which is fairly large for such a simple task).

So I wanted to write my own. I wanted to apply class names to inputs, textareas, selects, checkboxes and radios and have the form validated without any javascript written into the html at all.

Just as a bit of technical stuff here, it was easy enough to add a listener to the form submissions, but not easy to stop the default action (ie, return false) to the form this way. A forum gave me advice to use the YUI library with it’s event handling to stop the event. And it works, yay! So I guess I too have a package to accompany my script, but the two files required are very small compared to the Protoype script.

How to use

It’s simple enough. In your page, reference the YUI library and event scripts (v0.12 are included in the package for you). Also reference the jsvalidation.js file, which will perform the checking.

Next, create your form. Try to be XHTML compliant, for all of us! Now depending on the type of form elemen you create:

  • text, password and textareas
    Add these classes to the element. You can add more than one class to an element, seperating them with a space. Don’t add two contradicting validations though.

    • required – Will make this field required. Reccomended to use with other classes too.
    • validate-number – Field contents must be a number
    • validate-digits – Field contents must be digits, but spaces are also allowed
    • validate-alpha – Field contents must be alphabetical characters only
    • validate-alphanum – Field contents can be alphbetical and numerical, no punctuation
    • validate-date – A valid javascript date value
    • validate-email – Field must be an email address
    • validate-url – Field must be a URL (including the http://)
    • validate-date-au – A valid date formatted as dd/mm/yyyy
    • validate-currency-dollar – A valid dollar value
  • select elements
    Add these classes to the select element. You can add both if you like.

    • validate-not-first – Make the user choose an option that is not the first on the list
    • validate-not-empty – Make the user choose an option that has a value that is not null
  • radio and checkboxes
    Add this class to only one of the options in a group of radio inputs or checkbox inputs.

    • validate-one-required – At least one checkbox/radio element must be selected in a group

Error messages are defined by adding the title attribute to the same element as the class name with validation. If no title has been specified for any of the failed elements, a friendly message will appear to hide your lazyness.

After validation

The script will check the form elements for valid entries, and apply an additional class name to the elements depending on the success or failure of the validation. For example:

  • text, textarea, password: Adds validation-passed on success, and validation-failed on failure
  • select elements: Adds validation-passed-sel on success, and validation-failed-sel on failure
  • radio and checkboxes: Adds validation-passed-cr on success, and validation-failed-cr on failure

The reason I created different classes for the types of elements is for the flexibility of the designer. You may want a red background for a failed text element, but not for a failed select element.

Also, a messagebox will be displayed with a list of defined error messages, or a simple message if no specific error messages were defined.

Features of the script

In addition to just form validation, this script is intuitive. It will not try to validate hidden fields, or even fields that are not clearly visible to the user (hidden by CSS properties).

I’ve also added a minimum length option to text, textarea and password fields. Similar to the maxlength attribute, you can insert a minlength=”x” attribute to these inputs.

This script is not a class. But merely functions. There are some dependancy functions it requires that I have written myself, including:

  • addClassName(element,class)
  • removeClassName(element,class)
  • isVisible(element)
  • string trimming functions

If you’re a javascript programmer, you may find these useful. The source is also included with the package.

Download and Preview

Update:
Version 2 has a few fixes and now includes a validate-regex class you can apply. This only applies to text fields, and requires a regex= attribute to be applied to the element.

Another Update:
A Prototype version of this script is now available (and it’s smaller!).

Technorati: yahoo, yui, javascript, validation, unobtrusive

60 Comments »