| 1 | /** |
|---|
| 2 | * Creates a new cookie if the given cookie name does not exist. |
|---|
| 3 | * Sets a the cookie with the given name if it already exists. |
|---|
| 4 | * The cookie that is created or set expires at the end of the session. |
|---|
| 5 | * @param name {String} The name of the cookie. |
|---|
| 6 | * @param value {Any} The value of the cookie. |
|---|
| 7 | */ |
|---|
| 8 | function setCookie(name, value) |
|---|
| 9 | { |
|---|
| 10 | createCookie(name, value); |
|---|
| 11 | } |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * Creates a new cookie if the given cookie name does not exist. |
|---|
| 15 | * Sets a the cookie with the given name if it already exists. |
|---|
| 16 | * If days is not given, the cookie expires when the session ends. |
|---|
| 17 | * @param name {String} The name of the cookie. |
|---|
| 18 | * @param value {Any} The value of the cookie. |
|---|
| 19 | * @param days {Integer} (Optional) The number of days before the cookie expires. |
|---|
| 20 | */ |
|---|
| 21 | function createCookie(name, value, days) |
|---|
| 22 | { |
|---|
| 23 | if (days) |
|---|
| 24 | { |
|---|
| 25 | var date = new Date(); |
|---|
| 26 | date.setTime(date.getTime()+(days*24*60*60*1000)); |
|---|
| 27 | var expires = "; expires="+date.toGMTString(); |
|---|
| 28 | } |
|---|
| 29 | else |
|---|
| 30 | { |
|---|
| 31 | var expires = ""; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | document.cookie = name+"="+value+expires+"; path=/"; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * Reads the value of the cookie with the given name. |
|---|
| 41 | * @param name The name of the cookie to be read. |
|---|
| 42 | * @return The value of the cookie with the given name. |
|---|
| 43 | */ |
|---|
| 44 | function readCookie(name) |
|---|
| 45 | { |
|---|
| 46 | var nameEQ = name + "="; |
|---|
| 47 | var ca = document.cookie.split(';'); |
|---|
| 48 | |
|---|
| 49 | for(var i = 0; i < ca.length; i++) |
|---|
| 50 | { |
|---|
| 51 | var c = ca[i]; |
|---|
| 52 | while (c.charAt(0) == ' ') |
|---|
| 53 | { |
|---|
| 54 | c = c.substring(1,c.length); |
|---|
| 55 | } |
|---|
| 56 | if (c.indexOf(nameEQ) == 0) |
|---|
| 57 | { |
|---|
| 58 | return c.substring(nameEQ.length,c.length); |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | return null; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * Deletes the cookie with the given name. |
|---|
| 67 | * @param name The name of the cookie to delete. |
|---|
| 68 | */ |
|---|
| 69 | function eraseCookie(name) |
|---|
| 70 | { |
|---|
| 71 | createCookie(name, "", -1); |
|---|
| 72 | } |
|---|