• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 35. Cookies > John Krutsch's Cookie Jar

John Krutsch's Cookie Jar

The key to using cookies effectively relies upon the cookie functions you use. Experienced JavaScripters can create their own functions to read and manipulate cookies. John Krutsch, for instance, prefers to use his set of cookie functions, which he has graciously donated for this book. Pay close attention to the documentation within the code; it explains what each function cdoes.

Listing 35.3. Standard Functions For Setting, Getting, and Deleting Cookies

<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title></title>
<script language="JavaScript" type="text/javascript">
<!--// The Cookie Jar
////// Standard Functions for Setting, Getting, and Deleting Cookies
////// John Krutsch, john@xcentrixlc.com

/* Alterations by Alexander J. Vincent (Nov. 2001)

(1) Streamlined comments.
(2) Unescape cookie values in GetCookie added.
(3) Added alerts and "escape hatch" returns for missing required arguments.
*/

/*
To set a cookie pass in the name, value SetCookie("name", "value")
The 'name' and 'value' are the only required arguments.

If you want the cookie to persist past the current browser session, you can
specify the length of time you want to store the cookie in milliseconds:
SetCookie("name", "value", 15768000000) would store the cookie for six months.

When working with milliseconds you can calculate very specific expiration
dates; for example, you would calculate one month of milliseconds like so:

31 * 24 * 60 * 60 * 1000=2678400000

By omitting the expiration date the cookie will only last for the current
browser session.

To specify the domain which can use and affect this cookie specify it as
the fourth argument:

SetCookie("name", "value", 15768000000, ".xcentrixlc.com")
If the domain argument is omitted the domain of the document that set the
cookie will be used.

To specify the path which can use and affect this cookie specify it as the
fifth argument:

SetCookie("name", "value", 15768000000, ".xcentrixlc.com", "/myPath")

If the path argument is omitted the path of the document that set the
cookie will be used.

To set a secure cookie simply set the flag to true by including it as the
sixth argument:

SetCookie("name", "value", 15768000000, ".xcentrixlc.com", "/myPath",1)

If the secure argument is omitted the default of false is used.

To get or delete a cookie simply pass in the name of the cookie you want
to affect

GetCookie("name")
DeleteCookie("name")

If you ask for a cookie that does not exist it will return "".
*/

function GetCookie(cookieName) {
    if (arguments.length < 1) {
        alert("Not enough arguments!");
        return false;
    }
    var regX = new RegExp("\\b" + escape(cookieName) + "=([^;]*)");
    var found = regX.exec(document.cookie);
    return (found) ? unescape(found[1]): false;
}

function SetCookie(name, value, expires, domain, path, secure) {
    if (arguments.length < 2) {
        alert("Not enough arguments!");
        return false;
    }

    var cstr = escape(name) + "=" + escape(value);

    if (expires>0){
        var expdate = new Date();
        expdate.setTime(expdate.getTime() + expires);
        expdate = expdate.toGMTString();
        cstr+="; expires=" + expdate;
    }

    if(domain) cstr+="; domain=" + domain;
    if(path || path=="") cstr+="; path=" + path;
    if(secure) cstr+="; secure";

    document.cookie = cstr;
}

function DeleteCookie(name) {
    if (arguments.length < 1) {
        alert("Not enough arguments!");
        return false;
    }

    var exp = new Date();
    exp.setTime(exp.getTime() - 1000 * 60 * 60 * 24);
    document.cookie = escape(name)+"="+escape(name)+"; expires="+exp.toGMTString();
}

//-->
</script>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
document.write("<p>Adding author cookie...</p>\n")

SetCookie("author", "Alex Vincent")
document.write("<p>Author:" + GetCookie("author") + "</p>\n")

document.write("<p>Removing author cookie...</p>\n")
DeleteCookie("author")

document.write("<p>Author:" + GetCookie("author") + "</p>\n")
//-->
</script>
<!-- Results:
Adding author cookie...

Author:Alex Vincent

Removing author cookie...

Author:
-->
</body>
</html>


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint