Welcome to WebmasterWorld Guest from 54.144.77.26

Forum Moderators: open

Message Too Old, No Replies

Need Cookie Help

Javascript Impaired Webmaster

     

mbennie

10:11 pm on Sep 6, 2007 (gmt 0)

10+ Year Member



I need users to be able to set one or more pages as their favorite(s) then display links to their favorite pages with an option to remove it from their favorite list.

To accomplish this I need a javascript cookie that saves the url and anchor text in an array, then displays the data if they have the cookie along with a link to remove that selection from their list.

I'm completely retarded when it comes to javascript - any help?

mbennie

1:38 pm on Sep 7, 2007 (gmt 0)

10+ Year Member



I found some code on another site that works great.

Any idea how to have it display custom anchor text?

<html>
<script>

// basic get and set functions for javascript cookies
function setCookie (cookieName, cookieValue, expires, path, domain, secure) {
document.cookie =
escape(cookieName) + '=' + escape(cookieValue)
+ (expires? '; EXPIRES=' + expires.toGMTString() : '')
+ (path? '; PATH=' + path : '')
+ (domain? '; DOMAIN=' + domain : '')
+ (secure? '; SECURE' : '');
}

function getCookie (cookieName) {
var cookieValue = null;
var posName = document.cookie.indexOf(escape(cookieName) + '=');
if (posName!= -1) {
var posValue = posName + (escape(cookieName) + '=').length;
var endPos = document.cookie.indexOf(';', posValue);
if (endPos!= -1) {
cookieValue = unescape(document.cookie.substring(posValue, endPos));
} else {
cookieValue = unescape(document.cookie.substring(posValue));
}
}
return cookieValue;
}

// custom code to store and remove links from the cookies

function storePage(){
thisPage = document.location.href;
var pageList = getCookie("pageList");
// check to see whether this url is already in the cookie's links list
var linkFound = false;
if (pageList!= "" && pageList!= null) {
pSplit = pageList.split(";");
for (a=0;a<pSplit.length;a++) {
if (pSplit[a] == thisPage) linkFound = true;
}
}

// if url not already in cookie, then add it here
if (!linkFound) {
if (pageList == null pageList == ";") {
pageList = thisPage
} else {
pageList += ";" + thisPage;
}
}
// make the cookie expire in 1 years time:
var now = new Date();
var nextYear = new Date(now.getTime() + 1000 * 60 * 60 * 24 * 365);
setCookie("pageList",pageList,nextYear);

//refresh screen after link has been added
window.location.reload();
}

function removePage(url){
var pageList = getCookie("pageList");
var linkList = ""
// add each link to the linksList string and skip the one you want to remove
if (pageList!= "" && pageList!= null) {
pSplit = pageList.split(";");
for (a=0;a<pSplit.length;a++) {
if (pSplit[a]!= url && pSplit[a]!= '') linkList += ";" + pSplit[a];
}
}

// get the cookie expiry date
var now = new Date();
var nextYear = new Date(now.getTime() + 1000 * 60 * 60 * 24 * 365);
setCookie("pageList",linkList,nextYear);
// refresh screen after link has been removed
window.location.reload();
}

// code to write the list of urls to the page, and extra link to remove the url from the list.
function writeLinks(){
var pageList = getCookie("pageList")
if (pageList!= "" && pageList!= null) {
pSplit = pageList.split(";")
for (a=0;a<pSplit.length;a++) {
if (pSplit[a]!= '' && pSplit[a]!= 'null') {
document.write('<a href="' + pSplit[a] + '">' + pSplit[a] + '</a> <a href="javascript:removePage(\'' + pSplit[a] + '\')">[remove link]</a><br>');
}
}
} else {
document.write("There are no articles saved.");
}
}

</script>

<body>
<a href="javascript:storePage()">Store this page</a>

<p>
Your current links list is:<br>
<script>
writeLinks();
</script>

</body>
</html>

 

Featured Threads

Hot Threads This Week

Hot Threads This Month