root/trunk/common/Cookie.js

Revision 247, 4.3 kB (checked in by ydnar, 2 years ago)

bugid:60472; fix for cookie toggling in IE

  • Property svn:keywords set to Id
Line 
1/*
2Cookie JavaScript Library
3$Id$
4
5Copyright (c) 2007, Six Apart, Ltd.
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are
10met:
11
12    * Redistributions of source code must retain the above copyright
13notice, this list of conditions and the following disclaimer.
14
15    * Redistributions in binary form must reproduce the above
16copyright notice, this list of conditions and the following disclaimer
17in the documentation and/or other materials provided with the
18distribution.
19
20    * Neither the name of "Six Apart" nor the names of its
21contributors may be used to endorse or promote products derived from
22this software without specific prior written permission.
23
24THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35*/
36
37/* constructor */
38
39Cookie = 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
115override( 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} );
Note: See TracBrowser for help on using the browser.