how do I avoid the "undefined error" in cookie use last visit script?

i use the below script to welcome user on his first visit and show the last visit about the user ...i am using cookies ... it works well in IE, .. but in Mozilla there is undefined error displays,... how do i resolve that?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
 
function getCookieVal (offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
 
    return unescape(document.cookie.substring(offset, endstr));
}
 
function getCookie (name)  {
 
  
    var arg = name + "=";
 
    var argLength = arg.length;
    var cookieLength = document.cookie.length;
 
    var i = 0;
    while (i < cookieLength)  {
        var j = i + argLength;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j)
            i = document.cookie.indexOf(" ", i) + 1;
        }
        if (i == 0) {
            break
        }
     }
     return null;
}
 
var nameOfVisitor = getCookie('myCookie')
 
 
 
 
function getCookieVal (offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
 
    return unescape(document.cookie.substring(offset, endstr));
}
 
function getCookie (name)  {
 
  
    var arg = name + "=";
 
    var argLength = arg.length;
    var cookieLength = document.cookie.length;
 
    var i = 0;
    while (i < cookieLength)  {
        var j = i + argLength;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j)
            i = document.cookie.indexOf(" ", i) + 1;
        }
        if (i == 0) {
            break
        }
     }
     return null;
}
 
 
 
 
var nameOfVisitor = getCookie('myCookie')
 
if(nameOfVisitor != null) {
 
   var documentText = "<b>Welcome back</b> "
   documentText += nameOfVisitor
   documentText += "!"
}
 
else {
   var documentText = "<b>Welcome to this website!</b>"
}
 
document.write(documentText)
 
 
readCookie();
 
function readCookie() {
if (document.cookie == "") {
writeCookie();
alertMessage();
} else {
var the_cookie = document.cookie;
the_cookie = unescape(the_cookie);
the_cookie_split = the_cookie.split(";");
for (loop=0;loop<the_cookie_split.length;loop++) {
var part_of_split = the_cookie_split[loop];
var find_name = part_of_split.indexOf("nfti_date")
if (find_name!=-1) {
break;
} // Close if
} // Close for
if (find_name==-1) {
writeCookie();
} else {
var date_split = part_of_split.split("=");
var last = date_split[1];
last=fixTheDate(last);
document.write(" Your last visit was "+last);
writeCookie();
} // Close if (find_name==-1)
}
} // Close function readCookie()
 
 
function writeCookie() {
var today = new Date();
var the_date = new Date("December 31, 2023");
var the_cookie_date = the_date.toGMTString();
var the_cookie = "nfti_date="+escape(today);
var the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie=the_cookie
}
 
function alertMessage(){
document.write(" This is your first visit.");
}
 
function fixTheDate(date) {
var split = date.split(" ");
var fix_the_time = split[3].split(":")
var hours = fix_the_time[0]
if (hours>=12) {
var ampm="PM"
} else {
var ampm="AM"
}
if (hours > 12) {
hours = hours-12
}
var new_time = hours+":"+fix_the_time[1]+" "+ampm
var new_date = split[0]+" "+split[1]+", "+split[2]+" at "+new_time+", "+split[5]
return new_date;
}
 
 
cookie_name = "nullCookie";
var YouWrotea;
 
function getName() {
if(document.cookie)
{
index = document.cookie.indexOf(cookie_name);
if (index != -1)
{
namestart = (document.cookie.indexOf("=", index) + 1);
nameend = document.cookie.indexOf(";", index);
if (nameend == -1) {nameend = document.cookie.length;}
YouWrotea = document.cookie.substring(namestart, nameend);
return YouWrotea;
}
}
}
 
YouWrotea=getName();
if (YouWrotea == "nullCookie")
{YouWrotea = "Nothing_Entered"}
 
 
 
 
cookie_name = "prevCookie";
var YouWrote;
 
function getName() {
if(document.cookie)
{
index = document.cookie.indexOf(cookie_name);
if (index != -1)
{
namestart = (document.cookie.indexOf("=", index) + 1);
nameend = document.cookie.indexOf(";", index);
if (nameend == -1) {nameend = document.cookie.length;}
YouWrote = document.cookie.substring(namestart, nameend);
return YouWrote;
}
}
}
 
YouWrote=getName();
if (YouWrote == "prevCookie")
{YouWrote = "Nothing_Entered"}
 
 
 
if (YouWrote == 'undefined')
{YouWrote = ""}
var yes = YouWrote;
var other = YouWrotea;
 
if (yes == other) {
	document.write("");
	}
else {
	document.write("" +YouWrote+ "");
 
}
 
 
 
</script>
</head>
 
<body>
</body>
</html>

Open in new window

IE.JPG
mozila.JPG
LVL 36
Loganathan NatarajanLAMP DeveloperAsked:
Who is Participating?
 
spoxoxConnect With a Mentor Commented:
Your browsers are giving different answers when asked the current date in writeCookie. For example, Mozilla might be returning

Tue Jun 17 2008 13:14:11 GMT-0400 (Eastern Daylight Time)

while MSIE returns

Tue Jun 17 13:15:11 EDT 2008

Later, in the readCookie->fixTheDate function, the processing fails because it's expecting the MSIE format.


One solution is to:
1) process the date string during writeCookie so that fixTheDate has to worry about only one format.
2) update fixTheDate to deal with the standard format you decide on.


For some information on javascript date handling, see http://www.w3schools.com/JS/js_obj_date.asp

0
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
Ok, thanks, i will do and getback to u
0
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.