Wednesday, April 21, 2004

Blog News

Over the weekend, I'm going to adding approx. 16K to the site's file size. That's because I'm adding two linked stylesheets, and the little Javascript widget to enable you, my dear readers to switch between the styles. The switcher has a really 'dumb' cookie script enclosed within it's 1.67KB size.

I say the cookie script is dumb because it only 'remembers' but one thing. It remembers which of the alternate styles you last used whilst reading/mocking/shaking fist at/ this site.

If you know Javascript, you'll see that there is nothing nefarious about the cookie script.

Here's the cookie script:
function createCookie(name,value,days) 

{
if (days) {
var date = new Date();

date.setTime(date.getTime()+(days*24*60*60*1000));

var expires = "; expires="+date.toGMTString();
}
else expires = "";

document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');

for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);

if (c.indexOf(nameEQ) == 0)



return c.substring(nameEQ.length,c.length);
}

return null;
}

window.onload = function(e)
{
var cookie = readCookie("style");

var title = cookie ? cookie : getPreferredStyleSheet();

setActiveStyleSheet(title);
}


window.onunload = function(e)
{
var title = getActiveStyleSheet();

createCookie("style", title, 365);
}


var cookie = readCookie("style");

var title = cookie ? cookie : getPreferredStyleSheet();

setActiveStyleSheet(title);


Nothing to worry about. I was going to offer a 'switch cookies off' function, but I want to keep the code as lightweight as possible to keep page load times to a minimum.

No comments :