Click to See Complete Forum and Search --> : Name / Location Cookie


96turnerri
10-14-2003, 08:45 AM
hi im having a bit of trouble with a cookie i got in my site

i have this script for a cookie


<script>
mins = 60 /*number of minutes the cookie should persist*/
function writeCookie() {
/*write cookie*/
expdate = new Date()
expdate.setTime(expdate.getTime() + mins*60*1000);
document.cookie = "location="+window.location.href+"; expires=" + expdate.toGMTString() + "; path=/";
}
writeCookie();
</script>


which is in the head of every page and is read by


<script type="text/javascript">
function readCookie() {
/*cookie reading code*/
allcookies = document.cookie;
if (allcookies != "") {
/*check if a cookie named location exists*/
pos = allcookies.indexOf("location=");
if (pos != -1) {
var start = pos + 9;
var end = allcookies.indexOf(";", start);
if (end == -1) {
end = allcookies.length;
}
var value = unescape(allcookies.substring(start, end));
document.mistake.cookie.value = value;
}
}
}
readCookie();
</script>


which is displayed in a oneline box, i wish to do the same for this cookie which is placed on the homepage

but i cant get it to read on any other page can you please help?

<script type="text/javascript">
function getName(nm) {
userName = nm;
msg.close();
now = new Date();
exp = new Date(Date.parse(now) + (86400000*365));
SetCookie("userName", userName, exp);
window.location.reload();
}

function askUserName() {
msg = window.open("","msg","top=100,left=100,height=100,width=300,dependent=yes,resizable=yes");
msg.focus();
msg.document.writeln("<body onLoad='document.f1.nm.focus()' bgcolor='#000000'>");
msg.document.writeln("<form name='f1' onSubmit='window.opener.getName(this.nm.value);return false'>");
msg.document.writeln("<table>");
msg.document.writeln("<tr>");
msg.document.writeln("<td><font color='#FFFFFF'>Please enter your name: </font></td>");
msg.document.writeln("<td><input name='nm' type='text'></td>");
msg.document.writeln("</tr>");
msg.document.writeln("<tr>");
msg.document.writeln("<td>&nbsp;</td>");
msg.document.writeln("<td><input type='submit' value=' Submit '></td>");
msg.document.writeln("</tr>");
msg.document.writeln("</table>");
msg.document.writeln("</body>");
msg.document.close();
// window.location.reload();
return false;
}

function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

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

function GetCookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal(j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

userName = GetCookie("userName");
if (!userName)
{askUserName();}
//else
// {alert("Welcome, " + userName + "!");}

function checkUser() {
if (document.getElementById)
{if (userName) document.getElementById("d1").style.visibility = "visible";}
else {
if (document.layers)
{if (userName) document.d1.visibility = "visible";}
else
{if (userName) document.all.d1.style.visibility = "visible";}
}
}
</script>

Thanks
Rich

pyro
10-14-2003, 12:41 PM
Are all the pages in the same directory? If not, be sure you set the path parameter so the cookie is readable on all pages.

96turnerri
10-14-2003, 12:43 PM
yes all pages are in same directory, thanks for you help btw, what else could it be?

pyro
10-14-2003, 12:54 PM
So, if I understand you correctly, this works on the original page, but not on any of the others? Do you have a link?

96turnerri
10-14-2003, 01:09 PM
i wish the cookie that is placed on this pageto be read and displayed on a form (another page)

This is the page where the cookie is placed

Name (http://www.turnerskitchens.co.uk/name.htm)

Thanks Again

pyro
10-14-2003, 01:32 PM
It seemed to work fine on that page. Which page does it not work on?

96turnerri
10-14-2003, 01:39 PM
a form page i want the name in a online box, but i dont know how to get it to read the cookie

pyro
10-14-2003, 02:05 PM
You should already have code that will do it. Try modifying the readCookie or GetCookie function, and see if you can get one of them to work.