PDA

View Full Version : javascript / cookie trouble


althalus
08-21-2004, 08:43 PM
not sure where to begin with this one..

i recieved a theme-switcher script from willy duitt a while ago that works well in the example file, but not very well on the server. i would've asked him for help, but i just realised that he was catapulted some time ago, so.. :/

the script is as follows:

<script type="text/javascript">
<!--//
function setCookie(new_style) {
var which = document.getElementsByTagName('a');
var what = which[new_style-1].name;
var duitt = new Date();
duitt.setTime(duitt.getTime() + 365*24*60*60*1000);
var expString = "; expires=" + duitt.toGMTString();
document.cookie = 'My_Style='+what+expString;
}

function setStyle(which_style) {
if(/My_Style=style_(\d*)/.test(document.cookie)) {
which_style = RegExp.$1*1;
}
else {
which_style = which_style;
}

if(document.styleSheets) {
which_style = which_style-1;
for(i=0;i<document.styleSheets.length;i++) {
if(i!=which_style) {
document.styleSheets[i].disabled=true;
}
else {
document.styleSheets[i].disabled=false;
}
}
}
}
setStyle(1);
//-->
</script>

and is then run with

<a name="style_1" href="javascript:setStyle(1)" onclick="setCookie(1)">style one</a>
<a name="style_2" href="javascript:setStyle(2)" onclick="setCookie(2)">style two</a>
and so on...

it can be seen in "action" at http://mng.zinix.net .

any help is appreciated. thank you!

althalus
08-23-2004, 09:19 AM
*bump*

AaronCampbell
08-23-2004, 11:13 AM
I would do it like this:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="style_1.css">
<link rel="stylesheet" type="text/css" href="style_2.css">
<script language="javascript" type="text/javascript">
<!--
var now = new Date();
var expires = new Date();
/*
cookie expires in one year (actually, 365 days)
365 days in a year
24 hours in a day
60 minutes in an hour
60 seconds in a minute
1000 milliseconds in a second
*/
expires.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
document.cookie = curCookie;
}
function setStyle(which_style) {
if(which_style == 'cookie'){
which_style = getCookie('My_Style');
}
if(document.styleSheets){
which_style = which_style-1;
for(i=0;i<document.styleSheets.length;i++){
if(i!=which_style){document.styleSheets[i].disabled=true;}
else{document.styleSheets[i].disabled=false;}
}
}
}
//-->
</script>
</head>

<body onload="setStyle('cookie');">
<a href="javascript:setCookie('My_Style','1',expires,'/','.yourdomain.com',0);" onclick="setStyle(1);">Style Sheet 1</a><br />
<a href="javascript:setCookie('My_Style','2',expires,'/','.yourdomain.com',0);" onclick="setStyle(2);">Style Sheet 2</a>
Test
</body>
</html>
Make sure to change the red to your domain.

althalus
08-23-2004, 01:25 PM
yes that works better, thanks alot! however whenever i load a new page, style_1 gets loaded for a second before style_2 (having style_2 selected). does it have something to do with the
<link rel="stylesheet" type="text/css" href="style_1.css">
<link rel="stylesheet" type="text/css" href="style_2.css">

:confused:

appreciate your help!