Solved

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

Posted on 2008-06-17
3
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

626 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