Prototype Trim()

Posted by John
on Monday, 03 March 2008

Here's a nice little addon to the Prototype Ajax library that adds the ability to trim text, etc. with Prototype.

First add this to one of your .js include files,

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

now with protoype loaded you can trim your text simply and effectivly,

$('myfield').value.trim() == '';

this will simply get the value from the textbox 'myfield', trim it for whitespace and then compare it to see if it's empty.

Sweet!

Thanks goes to SOMACON.COM for this one.

Building ImageMagick 6.3.8 from source

Posted by John
on Sunday, 02 March 2008

Because Ubuntu 7.10 repositories don't have the latest version of ImageMagick, stopping you install the latest RMagick Ruby Gem; here's a guide to help you build it from source.

What had Gone Before

Before all this, I had installed the old version of ImageMagick by,

sudo apt-get install imagemagick
sudo apt-get install libmagick9-dev

So as you know before you start.

Remove ImageMagick

First off you're gonna have to remove your old copy of ImageMagick...

sudo apt-get remove imagemagick

Building & Installing ImageMagick 6.3.8

Now we're going to get the latest source files for ImageMagick and configure them correctly.

wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick-6.3.8-11.tar.gz
tar xvvzf ImageMagick-6.3.8-11.tar.gz
cd ImageMagick-6.3.8-11

Now we're in the source directory, G

./configure --prefix=/usr

Excellent, let's build...

sudo make

This will take a while, you may get a few warning pop up but hopefully no show-stoppers; afterwards lets install...

sudo make install

Rmagick Gem

And finally the latest RMagick Gem..

sudo gem install rmagick

If this still fails, you can install the old gem with..

sudo gem install rmagick -v=1.15.12

But fingers crossed let hope the new one works

Did it work?

Yes it all installed, and I've now got the RMagick 2.2.2 gem running on Ubuntu 7.10

So, What does it support?

Simple to find out, run the command below to see what version of ImageMagick you've got and what it can support.

identify -list format

Cookies + Persistence in JavaScript

Posted by John
on Monday, 04 February 2008

In this article we're going to play around with creating client-side cookies using JavaScript. First off here's a function to use which makes handling them easy.

JavaScript Cookie Library

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire + '; path=/');
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\s*' + escape(name) + '=([^;\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};

Usage

To create a cookie do,

Cookie.erase('mycookie'); Cookie.set('mycookie', 'myval', 365);

...365 is the days till expiry, we erase it before we set a new value just to be sure,

To erase,

Cookie.erase('mycookie');

To read a cookie,

var ii = Cookie.get('mycookie');

and if you get problems with bad numerics, try this,

var ii = Math.floor(Cookie.get('mycookie'));

or problems checking cookie exists, try this,

if (document.cookie.indexOf('mycookie=')>0) {

Persistence

Now persistence can be a pain in the rear if your cookies are getting cleared out when people navigate to somewhere else in your sites tree structure. So to make sure they stay around, set the path=/; this will force your cookies to be relevant right across your site. So if the person is in the photos area and then navigates down one they'll still be relevant and accessible via the functions above.

The function above does this already so you should be ready to roll if you use the code, ok my explanation's probably not 100% perfect but the codes working good this end, hope it does the same for you.