Style Switching Enlightenment

    Date: 08/16/06 (Javascript Community)    Keywords: java

    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();
    

    Source: http://community.livejournal.com/javascript/109471.html

« onChange this.form.submit || Opening Popups from Popups. »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home