Setting cookies with jQuery

Setting and clearing cookies with jQuery is really easy (especially when compared with regular Javascript) but it’s not includedin the jQuery core and requires a plug-in. This post shows how to set, get the value of and clear cookies with jQuery.

Changing the default text value on focus with jQuery

You’ve seen those cool search boxes or login fields on websites which have some default value (like e.g. "Enter keywords here") and when you click into the field the default text disappears but is then restored if you click out of the box without typing anything. This post shows how to change the default value of an HTML text field on focus and blur with jQuery.

Clear a form with Javascript

An HTML form can be reset using a reset button but this resets the form back to its original state, whereas you may want to be able to clear all the fields in the form. This post shows how to do this with Javascript.

Javascript getYear fix

The Javascript Date object method getYear() will return a 2, 3 or 4 digit value depending on the actual year specified and the browser version used. This post looks at two solutions for this Javascript getYear() issue.

The Javascript below will set month as an integer value between 0 (January) and 11 (December), day as an integer value between 1 and 31, and year to the current year.

var mydate = new Date();
 var month = mydate.getMonth();
 var day = mydate.getDate();
 var year = mydate.getYear();
 

The issue with the Javascript getYear() method is that for some dates and browser versions for dates prior to 2000 it may return either a 2 digit year or 4 digit year, and for dates since 2000 an integer value with 2 3 or 4 digits. The three digit ones would, for example be 108 for the year 2008.

The first solution is to test if the year is less than 2000 and if so add 1900 to it like so:

if(year < 2000) year += 1900;
 

The second solution is to use the newer getFullDate() method which always returns a 4 digit year.

var year = mydate.getFullYear();
 

Support for this method arrived in Javascript 1.3 which means (using the information from my “Which browsers support which versions of Javascript” post) all versions of Firefox, Opera from version 7.0 (and possibly earlier) and Internet Explorer from version 4.

Get a UNIX timestamp with Javascript

The Javascript Date object has a number of functions for working with dates and times. The getTime() method returns the millisecond representation of the Date object and this post shows how to convert this to a UNIX timestamp.