Monday 26 January 2009

Creating custom borders javascript function

I spent most of today writing a javascript function for adding custom borders to an element. It seems weird that it took most of the day, since it's not a particularly complicated or long function, and most of the postioning/css work I'd already done yesterday.

One part that took me quite a while was working out how to pass the variables to the function. I wanted it so you can just specify what variables you want to pass to the function and their values. Unfortunately you can't create variable variables like you can in php e.g. $myvar = 'hello';
$$myvar = 'goodbye';
echo $hello; //prints 'goodbye'
.

The nearest you can get to this is javascript is var myvar = 'hello';
window[myvar] = 'goodbye';
alert(hello); //alerts 'goodbye'
. Or if you don't want the variable to have global scope you have to do var myvar = 'hello';
this[myvar] = 'goodbye';
alert(this.hello); //alerts 'goodbye'
. This isn't as good as the php method as you can only have a global method or an object property.

In the end I decided to just pass an object to the function, then get the variables from the object e.g. function myfunc(params)
{
var hello = params.hello || 'defaultValue';
alert(hello);
}

myfunc({'hello':'goodbye'}); //alerts 'goodbye'
. Then in the actual function I set the vars I need to whatever they are set in the object, so I can pass the function variables to set each corner to a different height and width, or if all corners are the same width and height, I can just pass it the height of one corner and the background images it needs.

I'm not sure if the way I've done it is best, I will probably post on the websqueeze and ask the knowledgable people there.

In the morning I also played on Animal for about an hour and paid off my final mortgage. Nook said he'd put a flagpole outside my house. Now I need to save up to get a million in the bank, and also contribute towards the town fund.

In the afternoon I went on Animal again (just to check the afternoon turnip prices) and vacuumed my room. I also went in the garden for a bit. Where it was sunny it was nice standing outside in my T-shirt. There were some midgey style flies flying around, the honeysuckle by the green house had new leaves, and the Sallow bush had buds all over it, so Spring is coming, yay! Sadly though, there was a dead sparrow by the greenhouse. It looked okay so I think it must have just flown into the glass, although I couldn't see any splat marks.

In the evening I watched a bit of Battlestar Trashlactica with Mac (he came back from Worksop/Mansfield yesterday afternoon). It was too boring though so we only watched about an hour of it (it was a special about the Battlestar Pegasus).

After that me, Mac and Ben played a bit of Excite truck, then I played around with my custom borders javascript a bit more. The hard part is getting the images you want to use aligned and sized properly. I kept editing them, saving them, checking the page, then repeating this process until it worked properly. I probably could of just passed the different image dimensions to the function to get it to work (haven't tried this yet), but wanted to get the image correct since the images should be the same size.

The weather today was mainly cloudy and overcast. It brightened up in the afternoon, then clouded over again near sunset, but there wasn't cloud in front of the sun, so the sun lit up the bottoms of the clouds nice and orange as it set. I don't think the sky was very interesting after the sun had set though.

I listened to American folk/bluegrass music all day, mainly Iron Horse and New Grass Revival.

Food
Breakfast: Tangerine orange marmalade toast sandwich; cup o' tea.
Lunch: Mature cheddar cheese with salad sandwich; clementine (I think, I can't actually remember eating it, but I nearly always have some fruit for lunch); Raspberry kiss (Raspberry version of strawberry shortcake); cup o' tea.
Dinner: Pasta; meatballs; mixed veg; ground black pepper; grated parmesan cheese. Pudding was a hot cross bun with butter. Coffee; Cadbury's Dairy Milk Turkish.
Supper: Milk Chocolate digestive; cup o' tea.

No comments: