Forum Moderators: open

Message Too Old, No Replies

Styleswitcher cookie path not working in all dir.'s

         

JAB Creations

1:56 am on Jun 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I found a new stylesheet switcher that actaully sets the name of the cookie (and I can have it read the cookie and all). The problem is that even though I have the path set, all my attempts (attempts below, and then the script itself after it) fail because it only applies the new stylesheet to /home directory (site is in my profile). The semi-working setup I have now can be found in my site under "Home" at the top and then "Themes" on the left.

I'm not sure why it fails to apply the pages loaded in the frames from the mplayer directory if the cookie is set for the entire domain. Any ideas?

<a href="#" onmousedown="document.cookie='theme=Classic Theme;path=/;time()+2592000;' document.cookie='theme=Matrix Theme;path=/mplayer;time()+2593000;'" onclick="setActiveStyleSheet('Classic Theme'); parent.border.location.reload(); parent.copyright.location.reload(); parent.mplayer.location.reload(); parentcontent.location.reload(); return true;">Classic</a>
<h6>_~_</h6>
<a href="#" onmousedown="document.cookie='theme=Matrix Theme;path=/;time()+2592000;'; document.cookie='theme=Matrix Theme;path=/mplayer;time()+2593000;'" onclick="setActiveStyleSheet('Matrix Theme'); parent.border.location.reload(); parent.copyright.location.reload(); parent.mplayer.location.reload(); parentcontent.location.reload(); return true;">Matrix</a>

<br />
<br />
<br />
<a href="#" onmousedown="document.cookie='theme=Classic Theme;time()+2592000;/;/;'">classic create cookie</a>
<br/>
<a href="#" onmousedown="document.cookie='theme=Matrix Theme;time()+2592000;/;/;'">matrix create cookie</a>

<a href="#" onclick="setActiveStyleSheet('Classic Theme'); parent.border.location.reload(); parent.copyright.location.reload(); parent.mplayer.location.reload(); parentcontent.location.reload(); return true;">Classic</a>
<h6> ~ </h6>
<a href="#" onclick="setActiveStyleSheet('Matrix Theme'); parent.border.location.reload(); parent.copyright.location.reload(); parent.mplayer.location.reload(); parentcontent.location.reload(); return true;">Matrix</a>
<br />

function setActiveStyleSheet(title) {
var i, a, main;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style")!= -1 && a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
}

function getActiveStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style")!= -1 && a.getAttribute("title") &&!a.disabled) return a.getAttribute("title");
}
return null;
}

function getPreferredStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style")!= -1
&& a.getAttribute("rel").indexOf("alt") == -1
&& a.getAttribute("title")
) return a.getAttribute("title");
}
return null;
}

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("theme");
var title = cookie? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
}

window.onunload = function(e) {
var title = getActiveStyleSheet();
createCookie("theme", title, 365);
}

var cookie = readCookie("theme");
var title = cookie? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

RonPK

7:44 am on Jun 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> document.cookie='theme=Classic Theme;path=/;time()+2592000;'

Whitespace, commas and semicolons are not allowed in cookie values, so you'll need to escape them:
theme=' + escape("Classic Theme") + ';path[...]
Use unescape() when you read the cookie.

The correct way to set the expiration date is by using the expires attribute, which takes a date in GMT format. Also, time() is not a javascript method.

So, maybe this will work better:

var expdate = new Date();
expdate = expdate.getTime() + 2592000;
document.cookie = 'theme=' + escape('Classic Theme') + ';path=/; expires=' + expdate.toGMTString();