Solved

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

Posted on 2008-06-17
3
345 Views
Last Modified: 2011-10-19
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
0
Comment
Question by:Loganathan Natarajan
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
spoxox earned 500 total points
ID: 21805431
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
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 21809827
Ok, thanks, i will do and getback to u
0
 
LVL 36

Author Closing Comment

by:Loganathan Natarajan
ID: 31468020
thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question