Hi, I'm trying to go through the WebMonkey cookies tutorial but I can't find the information I need when it comes to setting(and reading) complicated cookies.
Right now, I can set and read a basic cookie with a single piece of information.
Here's what I have so far:
Set The Cookie:
-----------------
<script>
function setCookie()
{
var
the_name = prompt("Enter a URL in the form: www.yahoo.com","");
var the_cookie = "javascript=username:" + escape(the_name);
document.cookie = the_cookie;
alert("Now go to the next page.");
}
</script>
<a href="#" onClick="setCookie(); return false;">set the cookie</a>
----------------------
Ok, now for READING the cookie:
---------------------
<script language="JavaScript">
<!-- hide me
function readCookie(name) {
// if there's no cookie, return false else get the value and return it
if(document.cookie == '') return false;
else return unescape(getCookieValue(name));
}
function getCookieValue(name)
{
// Declare variables.
var firstChar, lastChar;
// Get the entire cookie string. (This may have other name=value pairs in it.)
var theBigCookie = document.cookie;
// Grab just this cookie from theBigCookie string.
// Find the start of 'name'.
firstChar = theBigCookie.indexOf(name);
// If you found it,
if(firstChar != -1)
{
// skip 'name' and '='.
firstChar += name.length + 1;
// Find the end of the value string (i.e. the next ';').
lastChar = theBigCookie.indexOf(';', firstChar);
if(lastChar == -1) lastChar = theBigCookie.length;
// Return the value.
return theBigCookie.substring(firstChar, lastChar);
} else {
// If there was no cookie, return false.
return false;
}
}
// show me-->
</script>
<script>
var the_cookie = readCookie("javascript");
var broken_cookie = the_cookie.split(":");
var the_name_part = broken_cookie[1];
var broken_name = the_name_part.split(";");
var the_name = broken_name[0];
document.write('<a href=http://'+the_name+'>'+the_name+'</a>');
</script>
---------------------
Simple enough right? Well, now there's ONE more thing I need to know how to do.. and that's adding more information to the cookie. I need one more piece of information added to this cookie, it would be link_name.
This would allow people to set the link they want, and then when it is displayed, they could click on the link_name to go to their link.
What would I need to add to my code to be able to do that?
thanks a TON to anyone who can help me
-Chuck







Bookmarks