Click to See Complete Forum and Search --> : Placing a cookie of last page
96turnerri
09-29-2003, 07:14 AM
i have this link at the bottom of every page on my site
<script language="javascript" src="JS/mistake.js"></script>
<a href="javascript:openMistake()">Seen A Mistake</a>
it opens a form in which the user fills out and then submits if theve seen a mistake, what i would like is for a cookie to be placed for the last page visited so when this link is clicked the page name is displayed for what they are submitting a mistake for
i have little knowledge on cookies, could someone please help me out
Thanks in advance
Rich
The first thing to remember is 13% of users do not browse with JavaScript enabled. If you've considered this, and still want to do it, read on...
You'll have to add something like this in the <head> of every page (or better, an external .js file that you call on every page):
<script type="text/javascript">
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>
and then use this to read it out:
<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));
alert (value);
}
}
}
readCookie();
</script>
96turnerri
09-30-2003, 05:58 AM
ok thanks im trying it now, what ill do is create kind of a <NOSCRIPT> bit with a box where they type it in, on the main page with site requirements, it does recomend java enabled, and tells you if it is enabled.
thanks let you know the outcome
96turnerri
09-30-2003, 06:05 AM
ok got it working thanks, but is there way to get it on the page, eg either as text or text in an input box?
Originally posted by 96turnerri
is there way to get it on the page, eg either as text or text in an input box? Yep, any number of ways really. If you want it in an input box, you'd use this:
document.formname.inputname.value = value;
If you want it written on the page, this would work:
document.write(value);
Or, if you want to add it to an element on the page (that you've given an ID to):
document.getElementById("someid").innerHTML = value;
96turnerri
09-30-2003, 08:07 AM
ok iv tried the text box one renaming, putting the formname and inputname but cant get it to work where abouts in the script you gave me for reading cookie would i place it?
96turnerri
09-30-2003, 08:13 AM
ok thanks for your help ive figured it out now thanks again
Rich