views:

179

answers:

2

I am attempting to set a cookie on a particular page to be read on another page. I wish to know why the other page is not being sent the cookie. Examining what is going on shows that the cookie is being set, but is not being sent to the server. My understanding was that if the path of a cookie is not set, the cookie will be sent to any page on the domain, though I tried adding path=/ to the cookie in case that would help anyhow. Opera has the cookie tagged as "Only sent to creator" for whatever reason. I'm sure I'm missing something simple.

<script type="text/javascript">
    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());
    }
    setCookie("mycookie",document.location.href,7);
</script>


http://www.site.com/Folder/subfolder/page.aspx - Cookie set here
http://www.site.com/folder/page.aspx - Cookie should be sent here. Why isn't it?

A: 

It certainly won't work without explicitly setting path; it certainly should work if you are setting the path.

Jonathan Feinberg
+1  A: 

As you said yourself, add the path:

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

If it's not working, clear all cookies and start again. Old cookies without the path set might be messing something up.

Joel L
I typed something wrong when I tried to set the path in hopes of fixing it. :/
Brian