License Public Domain
Lines 34
Keywords
common (1) javascript (9)
Permissions
Owner: raziel
Viewable by Everyone
Editable by raziel
Hide
Solve a problem – Filter by language, license, keyword, owner, or search text to find code & info fast. Join Siafoo Now or Learn More

Cookie handler object Atom Feed 0

In Brief This is a object I use to manipulate cookies.... more
# 's
 1var cookie = function() {
2 var private_var;
3 function private_method() {
4 // do stuff here
5 }
6 return {
7 create : function(name, value, days) {
8 var obj = document.getElementById("mdsGfooterAboutWrapper");
9
10 if (days) {
11 var date = new Date();
12 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
13 var expires = "; expires=" + date.toGMTString();
14 } else {
15 var expires = "";
16 }
17 document.cookie = name + "=" + value + expires + "; path=/";
18 },
19 read : function(name) {
20
21 var nameEQ = name + "=";
22 var ca = document.cookie.split(';');
23 for ( var i = 0; i < ca.length; i++) {
24 var c = ca[i];
25 while (c.charAt(0) == ' ')
26 c = c.substring(1, c.length);
27 if (c.indexOf(nameEQ) == 0)
28 return c.substring(nameEQ.length, c.length);
29 }
30 return null;
31 },
32 erase : function(name) {
33 this.create(name, "", -1);
34 }
35 };
36}();

This is a object I use to manipulate cookies.

Creating a cookie

# 's
1<script type="text/javascript">
2
3 cookie.create("User", "John Doe", 3);
4 //This will create a cookie with the name 'User' with the value 'John Doe' that will expire in 3 days
5
6</script>

Reading a cookie

# 's
1<script type="text/javascript">
2
3 cookie.read("User");
4 //Returns "John Doe"
5
6</script>

Erasing a cookie

# 's
1<script type="text/javascript">
2
3 cookie.erase("User");
4 //This will erase the cookie
5
6</script>