Forum Moderators: open
page A
<html>
<body>
<script type="text/javascript">
var exdate=new Date();
exdate.setDate(exdate.getDate()+1);
document.cookie="bNumber=" +escape("1_2_3")+ ";expires="+exdate.toGMTString());
</script>
</body>
</html> Page B
<html>
<body>
<script type="text/javascript">
bNumber="bNumber"; c_start=document.cookie.indexOf("bNumber=")
if (c_start!=-1)
{
c_start=c_start + bNumber.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
bNumber=unescape(document.cookie.substring(c_start,c_end))
alert(" the string are " + bNumber);
}
</script>
</body>
</html>
document.cookie="bNumber=" +escape("1_2_3")+ ";expires="+exdate.toGMTString()); + " path=/"
These are functions I use
function setCookie( sName, sValue) {
var date = new Date();
var currYear = date.getYear();
if ( currYear < 1000 ) {
if ( currYear >= 100 ) { currYear -= 100; }
currYear += 2000;
}
date.setYear( currYear + 1 );
document.cookie = sName + "=" + escape(sValue) + "; expires="
+ date.toGMTString() + '; path=/';
}
function getCookie( sName ) {
var c = document.cookie;
var oS = "";
var nv;
if ( c ) {
var cs = c.split(";");
var i = 0;
while( i<cs.length ) {
nv = cs[i].split( "=" );
if ( nv[0].indexOf(sName)!= -1 ) { if ( nv[1] ) {oS = nv[1];} }
i++;
}
}
return oS;
}
document.cookie="bNumber=" +escape("1_2_3")+ ";expires="+exdate.toGMTString()); You have an extra closing parenthesis on the above - this will cause a JS error and prevent the cookie from being written.
As daveVk suggests, you should perhaps also set a path on your cookie. Without explicitly setting a path, the path will default to the current directory and if Page B is in a different directory it will not be able to see the cookie.
But watch your syntax (parenthesis and semicolons):
document.cookie = "bNumber=" + escape("1_2_3") + "; expires=" + exdate.toGMTString() + "; path=/"; However, apart from that, your code does work OK. But watch your end-of-statement semicolons (;) in your cookie reading script, as you are missing quite a few!