SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot diskhub's Avatar
    Join Date
    Jun 2004
    Location
    singapore
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    erase all cookies from my domain

    Hi, can anyone advice me on how can I write a javascript to erase all cookies from my website? I do not wish to go thru each cookie and set a expired time on it.

    Any easier way?
    I LOVE PHP!!!

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Javascript cookies (document.cookie) are not saved on your website, but on your visitor's computers.

  3. #3
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,423
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Loop through each each and set the expiration date to be in the past. When they close their browser they will be deleted.

  4. #4
    SitePoint Zealot diskhub's Avatar
    Join Date
    Jun 2004
    Location
    singapore
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do i loop thru the cookies? can anyone guide me?
    is it something like document.cookie[0]? and document.cookie.length
    I LOVE PHP!!!

  5. #5
    SitePoint Zealot diskhub's Avatar
    Join Date
    Jun 2004
    Location
    singapore
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone can advice me?
    I LOVE PHP!!!

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Let's work out how to do it then. What we're after is something like this.

    Code JavaScript:
    forEach(cookie.list(), function (c) {
        cookie.erase(cookie.name(c));
    });

    When looping through arrays, it's often easier to use a forEach function to help make the code clearer. There are plenty of complex forEach functions out there that handle objects and recursion, but for now here's a relatively simple one that nicely does the job for arrays.

    Code JavaScript:
    var forEach = function (list, fn) {
        for (var i = 0; i < list.length; i++) {
            fn(list[i]);
        }
    };

    Now there's just the cookie handling to go.

    There are a number of cookie handlers around, but I've taken one and modified it to include a couple of other useful cookie functions, such as being able to listing them all and getting the name of a cookie.

    Code JavaScript:
    var cookie = {
        list: function () {
            var list = document.cookie.split(';');
            for (var i = 0; i < list.length; i++) {
                while (list[i].charAt(0) === ' ') { // trim whitespace
                    list[i] = list[i].substring(1, list[i].length);
                }
            }
            return list;
        },
        name: function (c) {
            return c.split('=')[0];
        },
        get: function (name) {
            var list = this.list();
            for (var i = 0; i < list.length; i++) {
                if (this.name(list[i]) === name) {
                    return list[i].substring(name.length + 1, list[i].length);
                }
            }
            return null;
        },
        set: function (name, value, days) {
            var date;
            var expires = '';
            if (days) {
                date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                expires = '; expires=' + date.toGMTString();
            }
            document.cookie = name + '=' + value + expires + '; path=/';
        },
        erase: function (name) {
            this.set(name, '', -1);
        }
    };
    Last edited by paul_wilkins; Jan 6, 2008 at 00:04.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •