homepage Welcome to WebmasterWorld Guest from 54.167.10.244
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Need Cookie Help
Javascript Impaired Webmaster
mbennie




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

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




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

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>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved