Forum Moderators: open

Message Too Old, No Replies

Help me add a name to this cookie please! :-)

         

JAB Creations

1:28 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I keep messing with this but I can't figure out where or how to add a name to the cookie this JS creates! I check for cookie name with Firefox under options/privacy/cookies/viewcookies.

var s=document.cookie.split(":");
var v=s[0],f=(s[1])?s[1]:'css-theme-classic-du.css';
document.write('<link rel="stylesheet" type="text/css" href="http://www.jabcreations.com'+f+'">\n');//.css">\n');
function switchStyle (n,s){
var x=new Date();x.setTime(x.getTime()+(60*60*24*365));
document.cookie=s+':'+n+':'+';expires='+x.toGMTString()+';path=/';
history.go(0);
}

rocknbil

5:51 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



document.cookie=s+':'+n+':'+';expires='+x.toGMTString()+';path=/';

Here's where it's being named. The cookie name is whatever's stored in s and n:
s='foo';
n='bar';
"foo:bar:"

document.cookie=cookiename;path=path/it/is/valid/in;expires=expiration_in_GMT_format;

I don't think (or have never seen) document.cookies or session cookies in the cookies files, only persistent server-side cookies set with a valid expiration date (but that doesn't mean they don't go there.)

JAB Creations

8:26 am on Apr 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The cookies ISNT named ... at least Firefox doesn't think it is. Visit my site, once you've entered look at the cookies listed for my site and the only one thats not named is the one controlling the themes (css).

rocknbil

5:31 pm on Apr 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay let me rephrase that. :-) If a value were being passed to switchStyle - that is where it would be named.

Sorry I don't have time to fully explore - but did have a look and see no name for the cookie that was set.

The way I'd approach debugging this is probably

function switchStyle (n,s){
alert('n is ' + n + ' s is ' + s);
.....
}

Just a brief look at your code above, if nothing ever gets stored in n and s, the cookie will have only : for a name (and lo and behold, here's what I got from your site:)

0:/themes/css-theme-classic.css:www....

So chase down n and s and find out where they're not getting populated, hope that's some modechem of help.

JAB Creations

8:24 pm on Apr 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let me help you understand the level I'm currently at...

I kept putting name="namehere" in different places. So while I really do appreciate your time you are still talking Greek to me ... I only speak two languages, English and bad English. :-J

rocknbil

5:49 pm on Apr 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay well I'll try.

The cookie is created and named in the switchStyle routine. That is, you have to call this routine by some method, a link, a form, whatever. Notice the two letters in the parentheses:
switchStyle (n,s){ ....

These are parameters that the function accepts to do it's thing. So to name a cookie, you need to PASS something that goes into those variables. See my notes for 1, 2, and 3.

function switchStyle (n,s){
1 var x=new Date();x.setTime(x.getTime()+(60*60*24*365));
2 document.cookie=s+':'+n+':'+';expires='+x.toGMTString()+';path=/';
3 history.go(0);
}

<a href="#" onClick="switchStyle('foo','different_style.css');">Switch to Different Style</a>

When you click the link, it passes 'foo' and 'different_style.css' to the function. This stores foo in n and different_style.css in s.

Line 1 builds the cookie expiration date.

Line 2 creates the cookie name out of n and s, and in our example the cookie name should be "foo:different_style.css:"

Line three actually sets the cookie.
In my last post I said I get
0:/themes/css-theme-classic.css:www.... To understand why, you need to go back to the first two lines of code, before your switching function.

When the page loads, it runs the first two lines of your code. This is called inline execution because it's not "stored" in any function; it just runs when the page loads.

If there's a cookie (which there's not the first time) it initially gets stored in a temporary array s:

var s=document.cookie.split(":");

This is a short-circuit evaluation that says "Set v to the first itme of the array 's'. If there's a value for the second item in the array of 's,' set f to that value; if not, use 'css-theme-classic-du.css'."

var v=s[0],f=(s[1])?s[1]:c;
document.write('<link rel="stylesheet" type="text/css" href="http://www.jabcreations.com'+f+'">\n');//.css">\n');

The 0: means nothing or zero is stored in n. The part after that means that the style is indeed being set but it's set as a default by the first two lines of your script, after the page loads.

So I'm guessing that whatever calls switchStyle is not passing a first parameter.

Does that help or am I at the restaurant at the end of the universe?