Solved

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

Posted on 2008-06-17
3
344 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript export function not working in IE 15 38
Capture logon name 13 48
how can i extend ? 2 22
php convert date format 3 0
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now