Marco Krechting <ma*************@zonnet.nl> wrote in message news:c0***************************@news.versatel.n l...
Hi All,
I have a page with a list of hyperlinks.
I want to save information in a cookie about the fact that I entered an
hyperlink or not.
When I click one of the hyperlinks I want this stored in a cookie and a
small bullit shown in front of the hyperlink,
so when I reload the page I can immediately see which hyperlinks I already
visited that day.
The information should be cleared after a day. Or maybe a button which
clears the info in the cookie so I can
start from scratch.
Can this be done and how?
Regards
Marco
The Netherlands
This code creates one session cookie comprised of the coded indices of all the clicked links, so the data survives as
long as the browser is running. To extend the duration to one day, change
setCookie('myVLinks', vLinks, 0);
to
setCookie('myVLinks', vLinks, 1);
Not knowing what cookie code you use, I have included two simplified cookie functions to make a working example.
There is no need to add any code to the links and the cookie should be able to store hundreds of indices.
The string parameter passed to the initLinks function is the name of the cookie to be used. If the code is used on more
than one page, this parameter must be different on each.
For simplicity, it's restricted to browsers supporting .innerHTML
To test the code, save it as: visited.htm
<HTML>
<HEAD>
<SCRIPT type='text/javascript'>
function readCookie(cookieName)
{
var cOffset=-1,wantedData="", wantedEndPoint;
if(typeof document.cookie!='undefined')
{
if( (cOffset=document.cookie.indexOf(cookieName)) >-1 )
{
cOffset+=cookieName.length+1;
if( ( wantedEndPoint=(document.cookie.substring(cOffset, document.cookie.length).indexOf(';')) ) ==-1)
wantedEndPoint=document.cookie.length;
wantedData=document.cookie.substring(cOffset,cOffs et+wantedEndPoint);
}
}
return wantedData;
}
function setCookie(cookieName, cookieValue, daysDuration)
{
var cs="";
if(typeof document.cookie!='undefined')
{
cs=cookieName+"="+cookieValue;
now=new Date();
if(daysDuration)
cs+=";expires=" + new Date(new Date().setDate(now.getDate()+daysDuration)).toGMTS tring();
document.cookie=cs;
}
return cs;
}
function linkRecord(cookieName, idx ) /* append link index to cookie string */
{
var vLinks=readCookie(cookieName);
if(vLinks.indexOf("L"+idx+',')==-1)
vLinks += "L"+idx+',';
setCookie(cookieName, vLinks, 0);
}
function initLinks(cookieName)
{
for(var i=0, ll=document.links.length; i<ll; i++)
document.links[i].onclick=new Function("linkRecord('"+cookieName+"',"+i+");")
if( (vLinks=readCookie(cookieName))!='' ) /* prefix bullet if data available and link index found */
{
if(document.links && document.links[0].innerHTML)
for(var i=0, ll=document.links.length; i<ll; i++)
if( vLinks.indexOf( "L" + i +',')!=-1 )
document.links[i].innerHTML='.'+document.links[i].innerHTML;
}
}
</SCRIPT>
</HEAD>
<BODY>
<A HREF='visited.htm'>Link to this page</A><BR><BR>
<A HREF='visited.htm'>Link to this page</A><BR><BR>
<A HREF='visited.htm'>Link to this page</A><BR><BR>
<A HREF='visited.htm'>Link to this page</A><BR><BR>
<SCRIPT type='text/javascript'>
// Optional cookie deleter
document.write("<A HREF=\"javascript
:void(setCookie('myVLinks', '0', -1))\">Delete Cookie</A>");
initLinks('myVLinks'); /* use a different name in each document that uses the script*/
</SCRIPT>
</BODY>
</HTML>
--
Stephen Chalmers
http://makeashorterlink.com/?H3E82245A