PDA

View Full Version : Cookie only serving pages on root directory?!



mrmozer
04-18-2009, 11:00 PM
Hello there...I have this cookie for a multi-themed website. The cookie works great except it only works on pages that are on my root directory. Can someone please tell me how I can modify it to serve the purpose on all pages in my website?



// JavaScript Document
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

thetestingsite
04-19-2009, 12:59 AM
Please post a link to the page on your site that contains the problematic script so we can check it out.

See these threads for proper posting format when asking a question.
http://www.dynamicdrive.com/forums/showthread.php?t=6
http://www.dynamicdrive.com/forums/showthread.php?t=19533

mrmozer
04-19-2009, 01:26 AM
The problem occurs on the following two pages:

http://www.s239171386.onlinehome.us/contact/index.php
http://www.s239171386.onlinehome.us/portfolio/gallery/index.php

When setting a theme color in one page the cookie does not bring up that preference on the other pages. I think that the cause of this is that these two pages are not on the root directory of the website.

thetestingsite
04-19-2009, 01:37 AM
In your setcookie function, add the highlighted below:



function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/";
}


Not tested, but should work. Hope this helps.

After looking at your page again, I notice that you do not specify the expiredays variable for the setcookie function. If you edit your function to the following, it should work this time.



function setCookie(c_name,value)
{
var exdate=new Date();
exdate.setDate(exdate.getDate());
document.cookie=c_name+ "=" +escape(value)+";expires="+exdate.toGMTString()+";path=/";
}


Hope this helps.

mrmozer
04-19-2009, 01:56 AM
Negative! I thought of this exact thing too, but for some strange reason it is not working.

Any other ideas?


PS: I really appreciate this :)

thetestingsite
04-19-2009, 01:58 AM
I've edited the post above, and this time it should work. Let me know if not though.

mrmozer
04-19-2009, 02:12 AM
Apparently there's a syntax error on the line in red below I can't figure out what it is:


function setCookie(c_name,value)
{
var exdate=new Date();
exdate.setDate(exdate.getDate());
document.cookie=c_name+ "=" +escape(value)+";expires="+exdate.toGMTString())+";path=/";
}

thetestingsite
04-19-2009, 02:14 AM
Sorry, there was an extra ) in the code after I edited it. Below is correct for that line:



document.cookie=c_name+ "=" +escape(value)+";expires="+exdate.toGMTString()+";path=/";


Hope this helps.

mrmozer
04-19-2009, 02:20 AM
Nope. This isn't working at all... worse than before unfortunately.

The cookie isn't set right. By the way does the cookie have to be in the root directory? because right now its sitting in /javascript/cookie.js

thetestingsite
04-19-2009, 02:24 AM
It shouldn't be a problem. After thinking about it a little bit, I think it may have something to do with the expiration date being set on the cookie. I'm not 100% on this, but perhaps someone else may be able to shed some light on this. Sorry I can't be of more help at the moment, but I will try to figure this out and let you know if I do.

ok, last try. Add the highlighted below to the following:



exdate.setDate(exdate.getDate()+30);

mrmozer
04-19-2009, 02:50 AM
Thanks a lot, friend! It works perfectly now with your last modification to the code.

Well done! :)