MLNTN

Maniacal musings of a pixel perfectionist

Archive for the ‘Javascript’ Category

jQuery IP address plugin

Posted by Jared On December - 30 - 2009

It’s pretty tough to handle IP addresses in HTML forms. Sure, you can do have 4 separate inputs and have users tab between them. You could setup one input and validate it on the server side. You could even use a tool like Masked Input to force users to match a specific format.

But all of those solutions fall short in one way or another. Read the rest of this entry »

jQuery undo plugin

Posted by Jared On March - 9 - 2009

I got a crazy idea when I was working with some AJAX interfaces – a quick and painless jQuery undo plugin.

What does that mean? I’m not entirely sure. Read the rest of this entry »

jQuery zoom event plugin

Posted by Jared On December - 11 - 2008

I don’t normally write code that I’m not ready to use, but I made an exception for this jQuery plugin.  It doesn’t really do all that I wanted it to do, but it’s good enough for a release.
Read the rest of this entry »

The start of a jQuery zoom event handler

Posted by Jared On December - 11 - 2008

After much research and very little code, I’ve come up with a basic start to a jQuery zoom event handler.  Don’t laugh.  This is very basic, but it handles zoom events based on keyboard shortcuts and fires the handler.
Read the rest of this entry »

Simple date conversion from SQL to readable

Posted by Jared On December - 2 - 2008

I needed a simple date converter function from SQL dates (YYYY-MM-DD) to something readable (M/D/YYYY).  So I wrote this little function.  Maybe someone else will have some use for it.

var makeSQLDatePretty = function(d) {
  return d.replace(/([0-9]{4})-0?([0-9]{1,2})-0?([0-9]{1,2})/, '$2/$3/$1');
};

Pass in a SQL date and the function trims leading zeros and returns a reformatted date.

var sqlDate = '2008-12-02';
alert(makeSQLDatePretty(sqlDate)); // should alert '12/2/2008'

Crazy Europeans may want to change the replace regex with ‘$3/$2/$1′.

Smart, unobtrusive AJAX form submission with jQuery

Posted by Jared On September - 22 - 2008

A former coworker was telling me recently about MooTools and how the AJAX functions are so easy to use.  He sent me the following example:
Read the rest of this entry »

Iterating through objects, the easy way

Posted by Jared On May - 8 - 2008

Objects in any ECMAScript language (Javascript, Actionscript, Jscript, etc) are very powerful for – among other things – storing data. However, getting some information out of these objects can be a hassle sometimes.

I ran into this issue recently and wrote some cool little handlers for objects. The following methods can be used to iterate through an array manually. I wrote them for Actionscript, but they certainly work for Javascript – although I’d suggest one small modification (shown later). Read the rest of this entry »

Image onerror magic

Posted by Jared On March - 26 - 2008

HTML has a little-known event handler for <img> and <body> tags. I’m going to focus on <img> tags and how you can use this to clean up some 404s. Read the rest of this entry »

Internet Explorer’s attachEvent breaks ‘this’

Posted by Jared On February - 25 - 2008

First of all, let me just say that I love jQuery. It makes Javascript development so much faster. I’m sure that Prototype is just as easy for those that use that daily, but I prefer jQuery. And before you ask… Yes, I learned both.

That having been said, there are some applications where I cannot use a Javascript library and I need to use the (longer) Javascript methods. There’s nothing wrong with this – in fact, I find that I always learn something new. Such was the case today. Read the rest of this entry »

Method overloading

Posted by Jared On December - 4 - 2007

John Resig, one of my heroes and creator of the jQuery JavaScript library, wrote a great piece about overloading methods. He created a dead simple function that shoulders most of the work. Check it out:

function addMethod(object, name, fn){
  var old = object[ name ];
  object[ name ] = function(){
    if ( fn.length == arguments.length )
      return fn.apply( this, arguments );
    else if ( typeof old == 'function' )
      return old.apply( this, arguments );
  };
}

Here’s how you could use it to extend the User object that we created earlier.

addMethod(User.prototype, "find", function(){
  // Find all users...
});
addMethod(User.prototype, "find", function(name){
  // Find a user by name
});
addMethod(User.prototype, "find", function(first, last){
  // Find a user by first and last name
});

You can read more from John’s blog here.