PDA

View Full Version : Javascript - setting cookies



Jeff
04-17-2008, 03:27 AM
I believe I have the right idea, but I am having a hard time making it come together. The problem is to write the current time and date to a cookie during body onUnload. When the user comes back, we are supposed to check for a cookie. If a cookie exist, we are to write the date and time of last visit. If not, we are supposed to welcome the user to the site. When I run this in my Javascript debugger, it comes back with no errors. I'm sure there is something wrong, as it doesn't work. lol


<html>
<head>
<title>cookies</title>
<script type="text/javascript">
function getCurrentTime() {
var today = new Date()
var hour = ""
var exr = ""
if (today.getHours()==0) {
hour = 12
ext = "a.m."
} else if (today.getHours() < 12) {
hour = today.getHours()
ext = "a.m."
} else if (today.getHours() == 12) {
hour = 12
ext = "p.m."
} else {
hour = today.getHours() - 12
ext = "p.m."
}
document.cookies= "cookieName=testcookie; hour=hour.toString(); minute=today.getMinutes().toString(); mille=today.getMilliseconds().toString();pmam=ext)"
}
function getCurrentDate() {
var theday =new Date()
document.cookies= "cookieName=testcookie; monthofvisit=theday.getMonth() + 1; dateofvisit=theday.getDate(); yearofvisit=theday.getFullYear())"
}
</script>
</head>
<body onUnload = "getCurrentTime(), getCurrentDate()">
<script type="text/javascript">
if(document.cookies=true) {
document.write("You last visited us on: ", document.cookies(monthofvisit, dateofvisit, yearofvisit, hour, minute, mille))
} else {
document.write("Welcome this is your first visit to our site")
}
</script>
</body>
</html>

Jeff
04-18-2008, 01:42 AM
Anyone have any ideas??

AMDave
04-18-2008, 02:14 AM
Yes. I was reading up on this last night (because it's been a while since I played with cookies - yes I know, you shouldn't play with food :D ). I just PM'd you.

Steve Lux
04-18-2008, 11:40 AM
I read on Fox news that Madonna's husband started doing cookies and he's not interested in sex any more. (of course, it could simply be with her) Personally, I try to stay away from them.