PageRenderTime 28ms CodeModel.GetById 19ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/as3/docs/cookies.js

http://wiiflash.googlecode.com/
JavaScript | 73 lines | 39 code | 3 blank | 31 comment | 7 complexity | ce415dcac1c18d297c645014b20df0d4 MD5 | raw file
 1/**
 2 * Read the JavaScript cookies tutorial at:
 3 *   http://www.netspade.com/articles/javascript/cookies.xml
 4 */
 5
 6/**
 7 * Sets a Cookie with the given name and value.
 8 *
 9 * name       Name of the cookie
10 * value      Value of the cookie
11 * [expires]  Expiration date of the cookie (default: end of current session)
12 * [path]     Path where the cookie is valid (default: path of calling document)
13 * [domain]   Domain where the cookie is valid
14 *              (default: domain of calling document)
15 * [secure]   Boolean value indicating if the cookie transmission requires a
16 *              secure transmission
17 */
18function setCookie(name, value, expires, path, domain, secure)
19{
20    document.cookie= name + "=" + escape(value) +
21        ((expires) ? "; expires=" + expires.toGMTString() : "") +
22        ((path) ? "; path=" + path : "") +
23        ((domain) ? "; domain=" + domain : "") +
24        ((secure) ? "; secure" : "");
25}
26
27/**
28 * Gets the value of the specified cookie.
29 *
30 * name  Name of the desired cookie.
31 *
32 * Returns a string containing value of specified cookie,
33 *   or null if cookie does not exist.
34 */
35function getCookie(name)
36{
37    var dc = document.cookie;
38    var prefix = name + "=";
39    var begin = dc.indexOf("; " + prefix);
40    if (begin == -1)
41    {
42        begin = dc.indexOf(prefix);
43        if (begin != 0) return null;
44    }
45    else
46    {
47        begin += 2;
48    }
49    var end = document.cookie.indexOf(";", begin);
50    if (end == -1)
51    {
52        end = dc.length;
53    }
54    return unescape(dc.substring(begin + prefix.length, end));
55}
56
57/**
58 * Deletes the specified cookie.
59 *
60 * name      name of the cookie
61 * [path]    path of the cookie (must be same as path used to create cookie)
62 * [domain]  domain of the cookie (must be same as domain used to create cookie)
63 */
64function deleteCookie(name, path, domain)
65{
66    if (getCookie(name))
67    {
68        document.cookie = name + "=" + 
69            ((path) ? "; path=" + path : "") +
70            ((domain) ? "; domain=" + domain : "") +
71            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
72    }
73}