Read, write and delete cookies in Javascript

Read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/ **
* Returns the value of a cookie
*
* @ Example
Var res = * getCookie ('variable');
*
* /
name ) { getCookie function (name) {
document. cookie . indexOf ( name + "=" ) ; var start = document. cookies. indexOf (name + "=");
start + name . length + 1 ; var len = start + name. length + 1;
! start ) && ( name != document. cookie . substring ( 0 , name . length ) ) ) return ( null ) ; if ((! start) & & (name! = document. cookies. substring (0, name. length))) return (null);
start == - 1 ) return ( null ) ; if (start == - 1) return (null);
document. cookie . indexOf ( ";" , len ) ; var end = document. cookies. indexOf ("", len);
end == - 1 ) end = document. cookie . length ; if (end == - 1) end = document. cookies. length;
unescape ( document. cookie . substring ( len , end ) ) ) ; return (unescape (document. cookies. substring (len, end)));
}

Write

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/ **
* Set a cookie
*
* @ Example
*
* SetCookie ('variable', 'value', 10);
*
* /
name , value , expiredays , path , domain , secure ) { setCookie function (name, value, expiredays, path, domain, secure) {
new Date ( ) ; var expires = new Date ();
expires. getDate ( ) + expiredays ) ; expires. setDate (getDate expires. () + expiredays);
name + "=" + escape ( value ) + cookieString var = name + "=" + escape (value) +
? ";expires=" + expires. toGMTString ( ) : "" ) + ((Expires)? "Expires =" + expires. ToGMTString (): "") +
? ";path=" + path : "" ) + ((Path)? "Path =" + path: "") +
? ";domain=" + domain : "" ) + ((Domain)? "Domain =" + domain: "") +
? ";secure" : "" ) ; ((Secure)? "Secure": "");
cookieString ; document. cookieString = cookie;
}

Eliminate

1
2
3
4
5
6
7
8
9
10
11
12
13
/ **
* Delete a cookie
*
* @ Example
* DeleteCookie ('variable');
*
* /
name , path , domain ) { deleteCookie function (name, path, domain) {
getCookie ( name ) ) document. cookie = name + "=" + if (getCookie (name)) document. cookie = name + "=" +
? ";path=" + path : "" ) + ((Path)? "Path =" + path: "") +
? ";domain=" + domain : "" ) + ((Domain)? "Domain =" + domain: "") +
"Expires = Thu, 01-Jan-70 00:00:01 GMT";
}

6 comments to "read, write and delete cookies in Javascript"

  1. August 30, 2009 Paul :

    I did not understand one thing: I would like to put a button on my site that has the function to erase all cookies from my site so that the page will reload to update the html files.
    How can I do?

  2. August 30, 2009 Giovambattista Fazioli :

    @ Paul: You should be more clear ... as you can see from the code brought a cookie is deleted by name. So you should, first of all, know all the cookies you have written. However I did not understand what you mean by "will update the html files" ... maybe you want to get something else with cookies does not have much to do, I mean update ... explain the problem better :)

  3. August 31, 2009 Paul :

    OK then I'll try to be clearer. When I update the pages of my site, for example, I edit the index.html and on the FTP host. So far so good. When I go to my site, the home that is the index. Html is not changed. To see it changed many times I reload the page or delete cookies. For this I want to create a button that deletes the cookies issued from my site (of which I know the names) and then reload the page automatically.
    I hope I was clear :)

  4. August 31, 2009 Giovambattista Fazioli :

    @ Paul: I think your problem NOT depend on cookies. A cookie, in fact, can do many things but I doubt prevent the refresh of your page in the browser, unless it was specifically created a system of this type. What you should check, however, is:

    1) The settings of your web server about the cache
    2) The settings of your browser (perhaps using Explorer and then view section that determines how it should behave when the browser reloads the same page)
    3) If you entered the index.html nalla your TAG META to force a cache cleaning

    If you still want to try deleting your cookies by inserting the button that you told me, the procedure is quite simple: associate the button - or link - a Javascript function that deletes cookies to cookie with the function presented in this post, type:

    1
    "erase_all_cookie()" > Elimina tutti i Cookie < / button > < button onclick = "erase_all_cookie ()"> Remove All Cookies </ button >
    1
    2
    3
    4
    5
    6
    erase_all_cookie function () {
    ) ; deleteCookie ("var1");
    ) ; deleteCookie ("var2");
    / / ...
    ) ; deleteCookie ("Varna");
    }
  5. August 31, 2009 Paul :

    By 1000, however, ok my site is hosted on altervista. Maybe that's the prob.
    Since I'm here, I'd like a link exchange or banner exchange. Or if I may say a "trick" to make a site popular :)

  6. August 31, 2009 Giovambattista Fazioli :

    @ Paul:

    Or if I may say a "trick" to make a site popular

    Write (well), respond to and do not rush :)

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA