...

View Full Version : Display time of last visit script



arturex
12-27-2006, 03:29 PM
I have just inserted the "Display time of last visit script" from JavaScriptKit.com on my site. Instead of displaying the last visit time the script shows the current time of each visit. What is my error ? Thanks

PS: obviously the script works well at http://www.javascriptkit.com/script/script2/lastvisit.shtml

TripperTreats
12-28-2006, 03:33 AM
Did you copy the script verbatim from the site?
Do you use cookies in any other way on your site?

arturex
01-13-2007, 12:40 PM
yes de script was copy directly from site and cookies are not used in other way. Thanks

Philip M
01-13-2007, 06:20 PM
Check again carefully to make sure you have not accidentally deleted or changed anything in the script.

arturex
01-13-2007, 09:44 PM
Just repeat all operation, starting copy script from source and build a new test page. Problem remains ... The test page is located at http://www.carnaxidedigital.com/welcome.html. Thanks

david_kw
01-13-2007, 11:01 PM
I went the first time and it said something like welcome new user.

The next time it said

Welcome back visitor! Your last visit was on Saturday, January 13, 2007 3:00:28 PM

Is this fixed or am I misunderstanding the problem?

david_kw

arturex
01-14-2007, 01:37 PM
The issue is the date/time showed is no the last visit time but the current time...

david_kw
01-14-2007, 06:11 PM
Ahh I misread your initial post.

Well the real problem was the cookie didn't save the time when someone logged in so there was no way to get it back. It looks like the original code was to count how many times you have visited. So instead of saving that, I changed it to save the time you visited so that could be printed instead of the current time.

I put in some alerts so you can see what it is doing and I had to put some whitespace in the code so I could read it more easily.



<!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>
</head>

<body><script type="text/javascript">

/***********************************************
* Display time of last visit script- by JavaScriptKit.com
* This notice MUST stay intact for use
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and more
***********************************************/

var lastvisit=new Object();
lastvisit.firstvisitmsg="This is your first visit to this page. Welcome!" //Change first visit message here
lastvisit.subsequentvisitmsg="Welcome back visitor! Your last visit was on <b>[displaydate]</b>" //Change subsequent visit message here

lastvisit.getCookie=function(Name){ //get cookie value
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) { //if cookie found
return document.cookie.match(re)[0].split("=")[1]; //return its value
}
return "";
}

lastvisit.setCookie=function(name, value, days){ //set cookei value
var expireDate = new Date();
//set "expstring" to either future or past date, to set or delete cookie, respectively
expireDate.setDate(expireDate.getDate()+days);
var expstring=expireDate.toGMTString();
alert("expires = " + expstring);
document.cookie = name+"="+value+"; expires="+expstring+"; path=/";
}

lastvisit.showmessage=function(){
var ldate = lastvisit.getCookie("visitcounter");
if (ldate==""){ //if first visit
document.write(lastvisit.firstvisitmsg);
} else {
document.write(lastvisit.subsequentvisitmsg.replace("\[displaydate\]", ldate));
}
lastvisit.setCookie("visitcounter", new Date().toLocaleString(), 730); //set "visitcounter" to today's date and for 730 days (2 years)
}

lastvisit.showmessage()
alert("current time = " + new Date().toLocaleString());

</script>
</body>
</html>


david_kw

Philip M
01-14-2007, 06:16 PM
I have just inserted the "Display time of last visit script" from JavaScriptKit.com on my site. Instead of displaying the last visit time the script shows the current time of each visit. What is my error ? Thanks

PS: obviously the script works well at http://www.javascriptkit.com/script/script2/lastvisit.shtml

David - this had me baffled as the javascriptkit script and that of arturex are identical. How come it works at javascriptkit?

david_kw
01-14-2007, 07:03 PM
Apparently they aren't using their own code. Notice the message the webpage gets is

Welcome back. Your last visit was on Sun, Jan 14 2007 at 10:51

But the code says it should be

Welcome back visitor! Your last visit was on <b>[displaydate]</b>

The code they have to write the date on the webpage is



if (Count()==1){
document.write("This is your 1st visit to our page")
When()
}
else if (Count()>1)
document.write("Welcome back. Your last visit was on <b>" + When() +"</B>.")


Which is completely different from what the code is they say to copy and paste. I've never used javascriptkit before. Is this an unusual case?

david_kw

Philip M
01-14-2007, 07:20 PM
Ah ha! Mystery solved!! I have to say that I have never encountered this before with any source, and always assumed that the demo was the same as the offered script. Perhaps a mod should look into this.

david_kw
01-14-2007, 07:21 PM
Here is the actual code used on the javascriptkit page



<!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 type="text/javascript">
<!-- Begin Script

//Display Time of last visit script- Mattias Sjoberg
//Modified by JavaScript Kit (http://javascriptkit.com)
//Visit http://javascriptkit.com for this script

var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function When(info){
// When
var rightNow = new Date()
var WWHTime = 0;
WWHTime = GetCookie('WWhenH')

WWHTime = WWHTime * 1

var lastHereFormatting = new Date(WWHTime); // Date-i-fy that number
var intLastVisit = (lastHereFormatting.getYear() * 10000)+(lastHereFormatting.getMonth() * 100) + lastHereFormatting.getDate()
var lastHereInDateFormat = "" + lastHereFormatting; // Gotta use substring functions
var dayOfWeek = lastHereInDateFormat.substring(0,3)
var dateMonth = lastHereInDateFormat.substring(4,10)
var hours = "" + lastHereFormatting.getHours()
var year = lastHereFormatting.getYear()
if (year < 1000) year+=1900
var minutes = "" + lastHereFormatting.getMinutes()
if (minutes.substring(0,1) == minutes){
minutes = "0" + minutes
}
var WWHText = dayOfWeek + ", " + dateMonth + " " + year + " at " + hours + ":" + minutes// display

SetCookie ("WWhenH", rightNow.getTime(), exp)

return WWHText;
}

function Count(info){
var psj=0;
// How many times
var WWHCount = GetCookie('WWHCount')
if (WWHCount == null) {
WWHCount = 0;
}
else{
WWHCount++;
}
SetCookie ('WWHCount', WWHCount, exp);


return WWHCount+1;
}



function set(){
// VisitorName = prompt("Who are you?", "Nada");
// SetCookie ('VisitorName', VisitorName, exp);
SetCookie ('WWHCount', 0, exp);
SetCookie ('WWhenH', 0, exp);
}

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 alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
// This cookie is history
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();

}
// End Script -->
</script>
</head>

<body>
<script>
if (Count()==1){
document.write("This is your 1st visit to our page")
When()
}
else if (Count()>1)
document.write("Welcome back. Your last visit was on <b>" + When() +"</B>.")
</script>
</body>
</html>


david_kw

arturex
01-15-2007, 08:41 AM
Thanks to all people, for the help.

arturex
01-15-2007, 04:41 PM
I still need a minor change - with the correct version of script the date is not showed in local language (and the bug version does). What need to be changed. Thanks, once more.

david_kw
01-15-2007, 05:36 PM
It looks like the "correct" script does it's own formatting of the date. If you want it to be the Locale version then just look at the function When() and strip out anything to do with formatting the date.

The easy way is to make this change to the end of When()



return lastHereFormatting.toLocaleString();
// return WWHText;


That will leave in the old formatting code in case you want to go back to it.

david_kw

arturex
01-16-2007, 08:04 PM
David,
sorry, but I am doing something wrong for sure.
If you don't mind pls point me to the exact change(s) on the script.

Thanks for you help

david_kw
01-16-2007, 08:23 PM
Try this.



<!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 type="text/javascript">
<!-- Begin Script

//Display Time of last visit script- Mattias Sjoberg
//Modified by JavaScript Kit (http://javascriptkit.com)
//Visit http://javascriptkit.com for this script

var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function When(info){
// When
var rightNow = new Date()
var WWHTime = 0;
WWHTime = GetCookie('WWhenH')

WWHTime = WWHTime * 1

var lastHereFormatting = new Date(WWHTime); // Date-i-fy that number
var intLastVisit = (lastHereFormatting.getYear() * 10000)+(lastHereFormatting.getMonth() * 100) + lastHereFormatting.getDate()
var lastHereInDateFormat = "" + lastHereFormatting; // Gotta use substring functions
var dayOfWeek = lastHereInDateFormat.substring(0,3)
var dateMonth = lastHereInDateFormat.substring(4,10)
var hours = "" + lastHereFormatting.getHours()
var year = lastHereFormatting.getYear()
if (year < 1000) year+=1900
var minutes = "" + lastHereFormatting.getMinutes()
if (minutes.substring(0,1) == minutes){
minutes = "0" + minutes
}
var WWHText = dayOfWeek + ", " + dateMonth + " " + year + " at " + hours + ":" + minutes// display

SetCookie ("WWhenH", rightNow.getTime(), exp)

return lastHereFormatting.toLocaleString();
// return WWHText;
}

function Count(info){
var psj=0;
// How many times
var WWHCount = GetCookie('WWHCount')
if (WWHCount == null) {
WWHCount = 0;
}
else{
WWHCount++;
}
SetCookie ('WWHCount', WWHCount, exp);


return WWHCount+1;
}



function set(){
// VisitorName = prompt("Who are you?", "Nada");
// SetCookie ('VisitorName', VisitorName, exp);
SetCookie ('WWHCount', 0, exp);
SetCookie ('WWhenH', 0, exp);
}

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 alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
// This cookie is history
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();

}
// End Script -->
</script>
</head>

<body>
<script>
if (Count()==1){
document.write("This is your 1st visit to our page")
When()
}
else if (Count()>1)
document.write("Welcome back. Your last visit was on <b>" + When() +"</B>.")
</script>
</body>
</html>


david_kw

arturex
01-18-2007, 08:58 AM
*****
Thanks David. Works perfect.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum