Image

Imageex_wronghead997 wrote in Imagejavascript 😯confused

Style Switching Enlightenment

SOLVED by Imagescimon. Check out the answer here.

I'm teaching myself Javascript and trying to slap together my own version of a style sheet switcher. I used some of the ideas from this tutorial but tried to put my own spin on things. I developed in Firefox but the finished product only works in IE (tried FF/Win and Opera/Win) and I was wondering if anyone could clue me in on what was wrong.


My header:


the included Javascript:
function chooseStyle(newstyle)
{
	var expirationDate = new Date();
	expirationDate.setTime(expirationDate.getTime() + (1000*60*60*24*365));
	document.cookie = 'style=' + newstyle + '; expires=' + expirationDate.toGMTString() + '; path=/';
	window.location = window.location;
}

function loadStyle()
{
	if (document.cookie.indexOf('style=') != -1)
	{
		var styleloc1 = document.cookie.indexOf('style=') + 6;
		var styleloc2 = document.cookie.indexOf(';', styleloc1) + 1;
		if (styleloc2 == 0)
		{
			styleloc2 = document.cookie.length;
		}
		var thestyle = document.cookie.substring(styleloc1, styleloc2);
	}
	
	if (thestyle)
	{
		var i, a, main;
		
		for (i = 0; (a = document.getElementsByTagName('link')[i]); i++)
		{
			if (a.getAttribute('rel').indexOf('style') != -1)
			{
				if (a.getAttribute('href').indexOf(thestyle) != -1)
				{
					a.setAttribute('rel', 'stylesheet');
				}
				else
				{
					a.setAttribute('rel', 'alternate stylesheet');
				}
			}
		}
	}
}

window.onload = loadStyle();