View Full Version : Set Cookie Path

03-21-2004, 07:39 PM

function create_cookie_105(whichone){
document.cookie="imgpref="+whichone.join("::delim::")+";expires="+new Date(new Date().getTime() +



function write_cookie_data_105(){

var coo=document.cookie+";";
var sn="imgpref=";
var startcoo=coo.indexOf(sn);

var endcoo=coo.indexOf(";",startcoo);
var data=unescape(coo.substring(startcoo,endcoo)).split("::delim::");


document.write("No Cookie Data!!!")




<script type="text/javascript">


I intend to set a path for this cookie for the example above, so that it can be created from one domain and accessed from another domain.

Say for example : http://www.1.com/createcookie.html - creates the cookie

http://www.2.com/readcookie.html - reads the cookie

I've found this example where it says and speaks about path and domains. http://www.webreference.com/js/column8/functions.html
But i have no idea on how to implement it, as am new to javascript.

An Example provided would be awesome

Thanks to everyone reading this

Could anyone also provide me an example with the above code on how to set a path for "Folders" creating cookies from "A" folder and accessing the same from "B" folder.


03-21-2004, 07:50 PM
Have a look at the JavaScript FAQ sticky, and the threads linked from there.

Also note that many people deny third party cookies (cookies set for other domains than the originating).

Willy Duitt
03-21-2004, 07:51 PM
A cookie can only be read from the domain it was set on.


03-21-2004, 07:53 PM
Not quite. It can only be read on the domain it was set for. The cookie syntax allows setting for other domains than the originating. If the cookie is accepted, it will be readable only from that domain.

03-21-2004, 08:09 PM

Can you tell me the source to find the FAQ

Willy Duitt & liorean
I guess you both are right.

And if that's the case, then am in big trouble. As i have ran out of solutions.

There is one last hope that i can think of, but it needs some working out.

I am attaching a zip file that contains two html files. searched.html and iframe.html

iframe.html calls searched.html and the cookie is created :)

However, in my example i am asking iframe.html to call searched.html place on

So bcos, the domains are different, the cookie fails to create :(

Why does this happen ?

Can something be changed in either iframe.html or searched.html so that the cookie is generated.

Help Please!


03-21-2004, 08:19 PM
It's a sticky, and is in fact the top thread in this forum. IF you're lazy, you can use the folloowing link, though: <http://codingforums.com/showthread.php?s=&threadid=34358#post178017>

03-21-2004, 08:31 PM
I am new to this forum, so i havent yet paid much attention to the posts, i shall be browsing through most of the posts to help increase my knowledge, and to input what i know.

Thanks for the info and the link :) i will surely go through the same.

Can you help me with that attachment that i have uploaded out here. Do u know why the cookie isnt being created? am calling the cookie creating page through an iframe.

There's something mysteriously wrong with it

Any help would be greatly appreciated.


03-22-2004, 09:53 AM
There were no attachments. But definitely, you can only read the cookie if the domain it was set for is the same or a subdomain of the current domain.

For example, the cookie domain is set to .yahoo.com. All of its subdomains (mail.yahoo.com, photos.yahoo.com, groups.yahoo.com, etc) can share the cookie.

03-22-2004, 03:38 PM
Yep you peeps are right :rolleyes:

Anyways i completely turned around the system, but what the heck, i got things to work the way i wanted :thumbsup:


03-23-2004, 02:47 AM
Maybe you would like to share your solution in order for other people with the same problem to implement it too or at least to have an idea. That's what forum's for. :)