Palm Pre is launched

I’m not usually one to get too excited about mobile phones, but the Palm Pre has launched today (in the USA at least – we in the UK are still patiently waiting).

Whether or not it will be an “iPhone killer” is hotly debated on the net – my feeling is probably not just yet, but the new operating system Palm Web OS looks very promising for us developer types, and apparently devices with other form factors are in the making.

The developer programme has yet to be fully rolled out, but the gist is that the platform allows you to build applications for the phone using existing web technologies such as HTML and Javascript – a little like Adobe Air I guess.

Anyway – I think Palm are to be applauded for innovating in this area, and I hope it’s a great success.

JQuery AJAX Uploader Plugin

One of the features I wanted to include in our new CMS system was the ability for users to upload files via AJAX, without using a form post to reload the page, and so that they can begin uploading a new file while an upload is already in progress.

JQuery to the rescue!

The plugin does the following things:

  • Sets up the form so that when the user submits, an invisble iframe is added to the page, and the form post is sent to that
  • Your back-end programming (PHP in my case) outputs some info as JSON to indicate whether the upload was successful
  • The plugin polls the iframe until it has loaded and found some data

You can use the plugin by attaching it to an HTML upload form as so:

$('#myUploadForm').ajaxUpload({
  onComplete : function(data) {
    // data is the JSON output from your PHP script
  }
})

The following callbacks can be passed in:

  • onStart: this is called first, right when the user clicks the ‘submit’ button
  • loading: this is called next, when the form has been posted and we’re waiting for the upload to complete
  • onComplete: this is called when the upload has been completed, and received the JSON output from your backend script as its argument

You can download the script here, or here it is below too:

/**
 * Author: James Carmichael
 * www.siteclick.co.uk
 *
 * If you use this plugin in your project, please kindly consider linking to the above
 *
 * JQuery ajax upload plugin
 *
 * v0.1 22/06/2008
 * v0.2 01/05/2009    Added 'onStart' function to settings, so upload start can be detected
 *
 * License: MIT
 */

jQuery.fn.ajaxUpload = function(settings) {

 // Set defaults
 var settings = jQuery.extend({
 'loading'    : function() {},
 'onComplete' : function() {},
 'onStart'         : function() {},
 'type'       : 'json'
 }, settings);

 var _this = this;

 // For each item
 return this.each(function(){

 jQuery(this).submit(function() {

 // Do onStart function
 settings.onStart(this);

 // Make up a number
 var iframeId = 'upload_' + Math.round(Math.random() * 1000000);

 // Create iframe for data transfer
 var iframe = jQuery('<iframe name="' + iframeId + '" id="' + iframeId + '"></iframe>');
 jQuery(document.body).append(iframe);
 jQuery(iframe).hide();

 // Set form action attribute
 jQuery(this).attr('target', iframeId);

 // Call the loading function
 settings.loading();

 /**
 * Function to poll iframe every second
 */
 _this.pollIframe = function() {

 // Get iframe HTML
 var html = jQuery('#'+iframeId).contents().find('body').html();

 // Data blank - try later
 if(!html) {
 window.setTimeout(_this.pollIframe, 1000);
 return;
 }

 // We have some data - send it to the oncomplete function
 switch(settings.type) {
 case 'json':
 var data = eval('('+html+')');
 settings.onComplete(data);
 break;
 }

 // Discard the iframe
 jQuery('#'+iframeId).remove();
 }

 // Begin polling
 window.setTimeout(_this.pollIframe, 1000);

 })

 })
}

A simple JQuery font chooser plugin

Update: I’ve now created a jQuery font picker v2

I needed a way to quickly and easily generate a list of web safe fonts for the theme editor in a new CMS I am working on.

So I quickly rustled up the following plugin – it’s very basic, and I initially had it showing the fonts in the select dropdown by adding CSS to the <option> tags – however good old IE didn’t seem to support this.

I hope to rewrite this plugin someday to show instead a nice div overlay with the actual fonts to choose from, but until then…


/**
* Font selector plugin
* turns an ordinary input field into a list of web-safe fonts
* Usage: $('select').fontSelector(options);
* You can pass in the following options:
* value        :    the initial value of the select field
* onChange :    a function to call when a new value is chosen
*
* Author     : James Carmichael
* Website    : www.siteclick.co.uk
* License    : MIT
*/
jQuery.fn.fontSelector = function(settings) {

var fonts = new Array(
'Arial,Arial,Helvetica,sans-serif',
'Arial Black,Arial Black,Gadget,sans-serif',
'Comic Sans MS,Comic Sans MS,cursive',
'Courier New,Courier New,Courier,monospace',
'Georgia,Georgia,serif',
'Impact,Charcoal,sans-serif',
'Lucida Console,Monaco,monospace',
'Lucida Sans Unicode,Lucida Grande,sans-serif',
'Palatino Linotype,Book Antiqua,Palatino,serif',
'Tahoma,Geneva,sans-serif',
'Times New Roman,Times,serif',
'Trebuchet MS,Helvetica,sans-serif',
'Verdana,Geneva,sans-serif' );

return this.each(function(){

// Store the input field
var sel = this;

/* add individual font divs to fonts */
jQuery.each(fonts, function(i, item) {

// Add option to selector
jQuery(sel).append('<option value="' + item + '">' + item.split(',')[0] + '</option>');

// Select initial value
if(item == settings.value)
jQuery(sel).children('option:last').attr('selected', 'selected');

})

// Bind onchange event handler
if(settings.onChange)
jQuery(sel).bind('change',    settings.onChange);

});

}

Slow right clicking in Windows Vista

I use Windows Vista for my sins, and one thing that was particularly frustrating recently was that simple act of right clicking. It seemed to take anything up to around a minute for the wretched context menu to appear in Windows Explorer, and finally I had had enough.

So I did a bit of hunting around and it seems that ‘Shell Extensions’ – little programs that affect the core functionality of the operating system, can balls things up big-stylee.

I found this incredibly useful application called ShellExView which once installed lets you browse these and disable them one by one.

If you click the ‘Type’ column you can then easily find all the ‘Context Menu’ shell extensions, and then right click and choose ‘Disable’. After disabling each in turn I tried right clicking in Windows Explorer to check the speed until the culprit was found.

The sinner turned out to be something to do with Macromedia Flash Paper viewer, and now I’m happily right clicking to my hearts content…

Hello, welcome and all that

So: every day I come across vast quantities of useful info on the web to do with web coding or other such geekery, and I thought it was about time to give something back to that community.

It didn’t seem quite right to put such a blog on our company website since it would probably bore most of our clients to death – so here I am instead.

I hope to add any useful tit-bits of info here relating to my coding activities such as:

  • PHP stuff
  • MySQL stuff
  • Javascript stuff
  • JQuery plugins
  • Links to useful things
  • Anything else that crosses my mind

Enjoy!