Click to See Complete Forum and Search --> : Please help - Cookie crisis!


Digirunt
06-17-2003, 11:23 AM
I have been trying for some time to solve this so any help here will be greatly appreciated;
I am trying to have a link on a number of pages on my site so the user can click and the name of the page (document.title) is saved to a cookie which can be accessed as a link from a favourites list on my home page.
The page names in question are the same as their filenames to make it easier. eg. My Poem is My Poem.html.
The poems with the link on however are in various sub-directories and my homepage is in the root.
The problems I am having are that the homepage cannot find the info from the cookies unless it is in the same directory as the pages with the link.
I am using a modified script I found on the net which references an external .js file for its functions.
i overcame this sort of by using seperate homepages in each directory (not ideal) and a main page with I-frames showing their content. However I now find that the cookies seem to erratically disapear or be overwritten when new links are added. I have tried several modifications to no avail.
All details are below.

*****************The external file contains:************

// <script language="JavaScript">

// <!-- Begin

// ** bookmark functions

var sepchar = "@"; // unique separator character
var BMtotal = 20; // max number of bookmarks permitted
var ShowCount = 0;
var expDays = 90; // cookie variables - expiry # days

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

function ListBookmarks()
{
var NumBookmarks = GetCookie('PT_NumBookmarks');
var i;
var Bookmark;

// alert("ListBookMarks started");

if (NumBookmarks == null)
{
NumBookmarks = 0;
}
ShowCount = 0; SwapColour = 0;

for (i=1; i <= NumBookmarks ; i++)
{
Bookmark = GetCookie('PT_Bookmark'+i);
if (Bookmark != null)
{
PrintBookmark(Bookmark, i);
}
}
}

function DeleteBookmark(Count)
{
DeleteCookie('PT_Bookmark'+Count);
window.location = window.location;
}

function PrintBookmark (Bookmark, Count)
{

var color = "";
//alert("hello 6");
var pairs = Bookmark.split(sepchar);
pairs[0]= unescape(pairs[0]);
pairs[1]= unescape(pairs[1]);
// alert(Bookmark+" "+sepchar);
var BMtitle = pairs[0];
var BMaddress = pairs[1];
//alert(BMaddress);

// SwapColour = 1 - SwapColour;
// if (SwapColour==1) {color = "bgcolor='#c0c0c0'"} ;

ShowCount++;
document.write("<tr " + color + ">");
document.write(ShowCount+"."+"&nbsp;");
document.write("<a "+"target='_parent' "+"href='"+BMaddress+"'><font face='arial' color='#000080'><small>"+BMtitle+"</small></a>"+"&nbsp;"+"&nbsp;");
document.write("<a href='javascript:DeleteBookmark(" + Count + ")'><small>Delete</small></a>"+"<br>");
}

function AddBookmark(BMtitle, BMaddress)
{

var NumBookmarks = GetCookie('PT_NumBookmarks');
var i;
var ToDoItem;
var Bookmark;
var OldestBookmark = 0;
var CountBookmarks = 0;

if (NumBookmarks == null)
{ NumBookmarks = 0; }

// check if already exists, and count bookmarks
for (i=1; i <= NumBookmarks ; i++)
{
Bookmark = GetCookie('PT_Bookmark'+i);

if (Bookmark != null)
{ CountBookmarks++;
if (OldestBookmark == 0)
{ OldestBookmark = i; }
}

if (Bookmark == BMtitle+sepchar+BMaddress)
{ alert("Bookmark added for "+BMtitle);
top.location.replace('../../bookmark.htm');
return; }
}


// now add it
NumBookmarks++;
//alert('PT_Bookmark'+NumBookmarks);
SetCookie('PT_Bookmark'+NumBookmarks, BMtitle+sepchar+BMaddress, exp);
SetCookie('PT_NumBookmarks',NumBookmarks, exp);
alert("Bookmark added for "+BMtitle);
top.location.replace('../../bookmark.htm');
//window.location = window.location;
}

// cookie functions

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);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

// End -->

// </script>


*********************END*************************

The add to favourites link is:

<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
if ( self.opener ){
}
else{
document.write("<a href='javascript:AddBookmark(document.title,window.location)'><img title='Add to favourites list' border='0' src='../../images/symbols/favorites.gif' width='36' height='36'></a>")
}
</SCRIPT>
**************************************************
//NOTE: All the other rubbish is to hide the link when the page is in a pop up as it auto redirects to the favourites page on submission and uses the top.location.replace as it was origionally in an I-frame.
**************************************************
The script on the homepage is:

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
ListBookmarks();
// End -->
</SCRIPT>

Can anyone solve this?
Am I just asking to much of Javascript?
Should I be using CGI or something else to achieve this?

Thanks