Click to See Complete Forum and Search --> : passing cookies from one page to another


flashgroover
09-12-2003, 01:20 PM
How do you pace cookies between pages on two different folders on a server? here's my code.

in folder_one is the file go1.htm -

<script language="JavaScript" type="text/JavaScript">

function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
document.cookie = curCookie;
}

function go(){

setCookie('thisone',1);
alert(getCookie('thisone'));
window.open('http://www.myserver.com/folder_two/go2.htm');

}

</script>

</HEAD>

<BODY>
<a href="javascript:go();">go</a>

</BODY>



In folder_two is the file go2.htm-

<script language="JavaScript" type="text/JavaScript">

function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}

function go2(){

alert(getCookie('thisone'));

}

</script>

</HEAD>

<BODY>
<a href="javascript:go2();">go2</a>
</BODY>

my problem is that the cookie 'thisone' gives a null value when it comes the recieving page 'go2.htm'. how do i get it to give the value i set of '1' on the first page? remember these files are in two different folders.
http://www.myserver.com/folder_one/go1.htm
http://www.myserver.com/folder_two/go2.htm

thank all yee masters of coding.

flashgroover

pyro
09-12-2003, 01:28 PM
You will want to set the path attribute to a / which means it will be available on the entire domain...

Khalid Ali
09-12-2003, 01:28 PM
don't have time to debug your code, however,if you can not get the cookie with the same name which us set on theprevious one,then obviously cookie is not set.

Makse sure cookie is set,and test it on the same page that you can retrieve the value on the page where you set it.Once validated then use the same function on the second page.