| 1 | /* |
|---|
| 2 | Cookie JavaScript Library |
|---|
| 3 | $Id$ |
|---|
| 4 | |
|---|
| 5 | Copyright (c) 2007, Six Apart, Ltd. |
|---|
| 6 | All rights reserved. |
|---|
| 7 | |
|---|
| 8 | Redistribution and use in source and binary forms, with or without |
|---|
| 9 | modification, are permitted provided that the following conditions are |
|---|
| 10 | met: |
|---|
| 11 | |
|---|
| 12 | * Redistributions of source code must retain the above copyright |
|---|
| 13 | notice, this list of conditions and the following disclaimer. |
|---|
| 14 | |
|---|
| 15 | * Redistributions in binary form must reproduce the above |
|---|
| 16 | copyright notice, this list of conditions and the following disclaimer |
|---|
| 17 | in the documentation and/or other materials provided with the |
|---|
| 18 | distribution. |
|---|
| 19 | |
|---|
| 20 | * Neither the name of "Six Apart" nor the names of its |
|---|
| 21 | contributors may be used to endorse or promote products derived from |
|---|
| 22 | this software without specific prior written permission. |
|---|
| 23 | |
|---|
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 25 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 26 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 27 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 28 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 29 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 30 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 31 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 32 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 33 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 34 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 35 | */ |
|---|
| 36 | |
|---|
| 37 | /* constructor */ |
|---|
| 38 | |
|---|
| 39 | Cookie = new Class ( Object, { |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * See <code>Cookie.bake</code> for doc on instantiation. This is a standard framework method.<br><br> |
|---|
| 43 | */ |
|---|
| 44 | init: function( name, value, domain, path, expires, secure ) { |
|---|
| 45 | this.name = name; |
|---|
| 46 | this.value = value; |
|---|
| 47 | this.domain = domain; |
|---|
| 48 | this.path = path; |
|---|
| 49 | this.expires = expires; |
|---|
| 50 | this.secure = secure; |
|---|
| 51 | }, |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Get this cookie from the web browser's store of cookies. Note that if the <code>document.cookie</code> |
|---|
| 56 | * property has been written to repeatedly by the same client code in excess of 4K (regardless of the size |
|---|
| 57 | * of the actual cookies), IE 6 will report an empty <code>document.cookie</code> collection of cookies. |
|---|
| 58 | * @return <code>Cookie</code> The fetched cookie. |
|---|
| 59 | */ |
|---|
| 60 | fetch: function() { |
|---|
| 61 | var prefix = escape( this.name ) + "="; |
|---|
| 62 | var cookies = ("" + document.cookie).split( /;\s*/ ); |
|---|
| 63 | |
|---|
| 64 | for( var i = 0; i < cookies.length; i++ ) { |
|---|
| 65 | if( cookies[ i ].indexOf( prefix ) == 0 ) { |
|---|
| 66 | this.value = unescape( cookies[ i ].substring( prefix.length ) ); |
|---|
| 67 | return this; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | return undefined; |
|---|
| 72 | }, |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Set and store a cookie in the the web browser's native collection of cookies. |
|---|
| 77 | * @return <code>Cookie</code> The set and stored ("baked") cookie. |
|---|
| 78 | */ |
|---|
| 79 | bake: function( value ) { |
|---|
| 80 | if( !exists( this.name ) ) |
|---|
| 81 | return undefined; |
|---|
| 82 | |
|---|
| 83 | if( exists( value ) ) |
|---|
| 84 | this.value = value; |
|---|
| 85 | else |
|---|
| 86 | value = this.value; |
|---|
| 87 | |
|---|
| 88 | var name = escape( this.name ); |
|---|
| 89 | value = escape( value ); |
|---|
| 90 | |
|---|
| 91 | // log( "Saving value: " + value ); |
|---|
| 92 | var attributes = ( this.domain ? "; domain=" + escape( this.domain ) : "") + |
|---|
| 93 | (this.path ? "; path=" + escape( this.path ) : "") + |
|---|
| 94 | (this.expires ? "; expires=" + this.expires.toGMTString() : "") + |
|---|
| 95 | (this.secure ? "; secure=1" : ""); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | var batter = name + "=" + value + attributes; |
|---|
| 99 | document.cookie = batter; |
|---|
| 100 | |
|---|
| 101 | return this; |
|---|
| 102 | }, |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | remove: function() { |
|---|
| 106 | this.expires = new Date( 0 ); // "Thu, 01 Jan 1970 00:00:00 GMT" |
|---|
| 107 | this.value = ""; |
|---|
| 108 | this.bake(); |
|---|
| 109 | } |
|---|
| 110 | } ); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | /* - - Static methods - - */ |
|---|
| 114 | |
|---|
| 115 | override( Cookie, { |
|---|
| 116 | fetch: function( name ) { |
|---|
| 117 | var cookie = new this( name ); |
|---|
| 118 | return cookie.fetch(); |
|---|
| 119 | }, |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | bake: function( name, value, domain, path, expires, secure ) { |
|---|
| 123 | var cookie = new this( name, value, domain, path, expires, secure ); |
|---|
| 124 | return cookie.bake(); |
|---|
| 125 | }, |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | remove: function( name ) { |
|---|
| 129 | var cookie = this.fetch( name ); |
|---|
| 130 | if( cookie ) |
|---|
| 131 | return cookie.remove(); |
|---|
| 132 | } |
|---|
| 133 | } ); |
|---|