-
Notifications
You must be signed in to change notification settings - Fork 1
/
cookie.js
73 lines (63 loc) · 1.89 KB
/
cookie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
( function ( win, doc, undef ) {
'use strict';
var stringentEncodeURIComponent = function ( str ) {
return encodeURIComponent( str )
.replace( /!/g, '%21' )
.replace( /'/g, '%27' )
.replace( /\(/g, '%28' )
.replace( /\)/g, '%29' )
.replace( /\*/g, '%2A' );
},
stringentDecodeURIComponent = function ( str ) {
return decodeURIComponent( str )
.replace( /%21/g, '!' )
.replace( /%27/g, '\'' )
.replace( /%28/g, '(' )
.replace( /%29/g, ')' )
.replace( /%2A/g, '*' );
},
obj = function( name, value, params ) {
this.name = name;
this.value = value || this.get();
this.params = params || {};
};
obj.get = function ( name ) {
var matches = doc.cookie.match( /\w+=[^;]+/g ),
len = matches !== null ? matches.length : 0,
ret = [],
cookie,
i;
for ( i = 0; i < len; i++ ) {
cookie = matches[ i ].match( /(\w+)=(.+)/ );
if ( name === cookie[ 1 ] ) {
ret.push( stringentDecodeURIComponent( cookie[ 2 ] ) );
}
}
return ret;
};
obj.set = function ( name, value, params ) {
params = params || {};
doc.cookie = name + '=' + stringentEncodeURIComponent( value ) +
( params.expires !== undef ? '; expires=' + params.expires : '') +
( params.max_age !== undef ? '; max-age=' + params.max_age : '' ) +
( params.domain !== undef ? '; domain=' + params.domain : '' ) +
( params.path !== undef ? '; path=' + params.path : '' ) +
( params.secure ? '; secure' : '' );
return value;
};
obj.clear = function ( name, params ) {
params = params || {};
params.max_age = 0;
obj.set( name, undef, params );
};
obj.prototype.get = function () {
return obj.get( this.name );
};
obj.prototype.set = function ( value, params ) {
return obj.set( this.name, value || this.value[ 0 ], params || this.params );
};
obj.prototype.clear = function ( params ) {
return obj.clear( this.name, params );
};
win.Cookie = obj;
}( window, document ) );