Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, 22 March 2010

Cookie handling

Writing a cookie:

(function(){

// erase any existing instances
eraseCookie('nameOfCookie');

// bind a change event to the cookie
$("input:radio[name:'radioName']").bind( 'change',function(){

//write the cookie
createCookie('extrasCookie',$(this).val(),7);
})
})();



Reading the cookie:

(function(){
if(readCookie('extrasCookie')=="1"){

// do something based on the results

}
})();

Tuesday, 26 January 2010

Strings in JavaScript

JavaScript has a really crappy algorithm for pushing Strings together so, where possible, you should use the inbuilt array join() method

var __string = [“string1”,“string2”,“string3”,].join();

instead of

var __string = “string1” + “string2” + “string3”

Monday, 18 January 2010

Simple slideshow - with jQuery

// set the current image to 1 - outside of the function
var current = 1;
OW.slideShow = function() {

// each time this function runs, fade out the image
$j(".className img").fadeOut(350,function(){
current++;
if(current==4){current=1;}

// then update the source of the image to the next one
$j(".className img").attr("src","images/content/img-placeholder-"+current+".jpg");

// then fade the image back in again
$j(".className img").fadeIn(350);
});

}

$j(document).ready(function(){

// in the document.read function attach a time onto the function - in this case it'll run every 6 seconds
if($j('.className img').length){var __int = setInterval("OW.slideShow()",6000);}

});

Thursday, 7 January 2010

Automatically populate a select box with date options 12 months from now

A function to take a select object, clear it out and then populate it with dates from the current month for a year (12 months).

function createDates(){
var mth = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

// previous function sets up f.$ as 'getElementById'
var m = f.$("in_month");
var ml = f.$("out_month");

// get the current Month
var currMth = current.getMonth();

// function is called from the two variables below
// an object is passed through, and all variables on the object are referenced through o

var setMonthSelect = function (o){
o.optMthList.options.length = 0
var y = current.getFullYear();

// cycle through 12 and add the 12 months into the array
for (var i=0; i < 12; i++){

//set up the variables that will go into the options - checkVal is the current month and is incremented in the 'if' statement below
var monthVal = o.optMth[o.checkVal] + '-' + y;
var monthText = o.optMth[o.checkVal] + ' ' + (y-2000);

// figure out if it should be the one that's selected (it's easy, the first one is..)
var sel;
i==0?sel=true:sel=false;

// the thing that actually does the work - function is below (and attribution)
AddSelectOption(o.optMthList, monthText, monthVal, sel);

//if the current month is Dec, make it Jan, otherwise add one
if (o.checkVal == 11){
o.checkVal=0
y++;
}else{
o.checkVal++;
}
}
};

// The calls
setMonthSelect({optMth:mth, checkVal:currMth, optMthList:m});
setMonthSelect({optMth:mth, checkVal:currMth, optMthList:ml});
}

// this function stolen from steven harman
function AddSelectOption(selectObj, text, value, isSelected){
if (selectObj != null && selectObj.options != null){
selectObj.options[selectObj.options.length] = new Option(text, value, false, isSelected);
}
}