...

View Full Version : How can I make a cookie counter appear on a seperate page into a textbox



TheShape
07-18-2004, 05:17 PM
I want to be able to click a link and have the value of the cookie counter for that page to show up on my index.htm page. I am able to get the cookie value show up inside a text box for the actual page I visist but I preffer if it shows up on the index.htm within a text box.
Im supposed to call the cookie somehow but I have no clue how.
This is what I got thus far:

<HTML>
<HEAD>
<TITLE>Chapter 1</TITLE>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--Hide from old browsers

function addCookie(tag, value)
{
var expireDate = new Date()
var expireString = ""
expireDate.setTime(expireDate.getTime() + (1000 * 60 * 60 * 24 * 365))
expireString = "expires=" + expireDate.toGMTString()
document.cookie = tag + "=" + escape(value) + ";" + expireString + ";"
}

function getCookie(tag)
{
var value = null;
var myCookie = document.cookie + ";"
var findTag = tag + "="
var endPos

if (myCookie.length > 0)
{
var beginPos = myCookie.indexOf(findTag)
if (beginPos != -1)
{
beginPos += findTag.length
endPos = myCookie.indexOf(";", beginPos)
if (endPos == -1)
endPos = myCookie.length
value = unescape(myCookie.substring(beginPos, endPos))
}
}
return value

}


var CookieValue=0
CookieValue = getCookie("VisitNumber")
if (CookieValue != null)
{
CookieValue=parseInt(CookieValue) + 1
}
else
{
CookieValue= 1
}
addCookie("VisitNumber",CookieValue)
//-->
</SCRIPT>
</HEAD>


<BODY>

<H2 ALIGN="CENTER"><FONT COLOR="darkblue">Page 1</FONT></H2>



<form name="form1">

<table>
<tr><td>
<input type="text" name="t1">
</td></tr>
</table>
</form>

<script language="javascript">
var CookieValue=""

CookieValue =getCookie("VisitNumber")
if(CookieValue !=null)
{
document.form1.t1.value= CookieValue;
}

</script>

</BODY>
</HTML>

Willy Duitt
07-18-2004, 07:36 PM
Place your getCookie function in the head of your index.htm page and place the script which you currently have on the bottom of page one...on the bottom of the index.htm page....

.....Willy

TheShape
07-18-2004, 09:03 PM
Thanks it actually worked but I have one problem now. The value shows up in the index.htm textbox but if I click the link again and go back to the index.htm the value doesnt change, I have to close the window and reopen to see the new value. How can I make it change everytime I refresh?

Another problem is if I have 5 links how can I make the cookie counter display the value in its own textbox for the index.htm. I tried copying the same code I used for link 1 to the other links but I end up receiving the same value for each text box.

By the way im looking for a cookie script to show me the last date and time someone viewed the page. I been looking but I keep getting scripts that are all in one. They show you the name of the person, the counter, the date and time and sometimes the brower they are using. I dont want all that I just want the last date and time.

<html>
<head>
<title>Index Form</title>

<script language="javascript">
function getCookie(tag)
{
var value = null;
var myCookie = document.cookie + ";"
var findTag = tag + "="
var endPos

if (myCookie.length > 0)
{
var beginPos = myCookie.indexOf(findTag)
if (beginPos != -1)
{
beginPos += findTag.length
endPos = myCookie.indexOf(";", beginPos)
if (endPos == -1)
endPos = myCookie.length
value = unescape(myCookie.substring(beginPos, endPos))
}
}
return value

}
</script>

</head>

<body bgColor="lightblue">
<h1 align="center">Index</h1>

<form name="indexForm">
<table border="1" align="center" cellspacing="3" cellpadding="3" width="50%" bgColor="lightgreen">
<tr>
<th>
Chapter
</th>
<th>
Last date and time visited
</th>
<th>
Number of times visited
</th>
</tr>

<tr>
<td>
<a href="1.htm">first link</a>
</td>
<td>
<input type="text" name="" size="23">
</td>
<td>
<input type="text" name="i1" size="5">
</td>
</tr>

<tr>
<td>
<a href="1.htm">second link</a>
</td>
<td>
<input type="text" name="" size="23">
</td>
<td>
<input type="text" name="i2" size="5">
</td>
</tr>

<tr>
<td>
<a href="1.htm">third link</a>
</td>
<td>
<input type="text" name="" size="23">
</td>
<td>
<input type="text" name="i3" size="5">
</td>
</tr>

<tr>
<td>
<a href="1.htm">fourth link</a>
</td>
<td>
<input type="text" name="" size="23">
</td>
<td>
<input type="text" name="i4" size="5">
</td>
</tr>

<tr>
<td>
<a href="1.htm">fifth link</a>
</td>
<td>
<input type="text" name="" size="23">
</td>
<td>
<input type="text" name="i5" size="5">
</td>
</tr>
</table>
</form>

<script language="javascript">
var CookieValue=""

CookieValue =getCookie("VisitNumber")
if(CookieValue !=null)
{
document.indexForm.i1.value= CookieValue;
}
</script>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum