SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript and Cookies

    I was looking for a good script to handle cookies and I finally end up with this:

    Code:
    var Cookie = (function() {
    /// <source>http://www.w3schools.com/JS/js_cookies.asp</source>
        return {
            cookiesEnabled : function() {
                return navigator.cookieEnabled;
            },
            createCookie : function(cookieName, value, /* [ optionals */ expiresIn, path, domain, secure) {
                var expireDate = new Date();
                expireDate.setDate(expireDate.getDate() + expiresIn);
                var optionals = ((path) ? ('; path=' + path) : '') + ((domain) ? ('; domain=' + domain) : '') + ((secure) ? ('; secure') : '');
                document.cookie = cookieName + '=' + escape(value)
                                  + ((expiresIn==null) ? '' : ';expires='+expireDate.toGMTString())
                                  + optionals;
            },
            readCookie : function(cookieName) {
                if (document.cookie.length > 0)
                {
                    c_start = document.cookie.indexOf(cookieName + '=');
                        if (c_start >= 0)
                        { 
                            c_start = c_start + cookieName.length+1; 
                            c_end = document.cookie.indexOf(';', c_start);
                            if (c_end == -1) {
                                c_end = document.cookie.length;
                            }
                            return unescape(document.cookie.substring(c_start, c_end));
                        }
                }
                return '';
            },
            deleteCookie : function(cookieName, path, domain, secure) {
                try {
                    var optionals = ((path) ? ('; path=' + path) : '') + ((domain) ? ('; domain=' + domain) : '') + ((secure) ? ('; secure') : '');
                    Cookie.createCookie(cookieName, ' ', -1, optionals);
                }
                catch(ex) {}
            }
        };
    })();
    After days of trials and errors I still can't make it work...
    I mean, as it is now I can create, read and delete a cookie, but not if I specify a path or domain...

    Ex:

    Code:
    Cookie.createCookie('Temp', 'Temp value');
    document.writeln('Cookie created.<br />');
    document.writeln('Cookie value #1: '+ Cookie.readCookie('Temp')); // outputs: Temp value
    Cookie.deleteCookie('Temp');
    document.writeln('Cookie deleted.<br />');
    document.writeln('Cookie value #2: '+ Cookie.readCookie('Temp')); // outputs: nothing
    
    Works as expected.

    When I choose to specify a path:
    Code:
    Cookie.createCookie('Temp', 'Temp value', 2, '/');
    document.writeln('Cookie created.<br />');
    document.writeln('Cookie value #1: '+ Cookie.readCookie('Temp')); // outputs: nothing
    Cookie.deleteCookie('Temp', '/');
    document.writeln('Cookie deleted.<br />');
    document.writeln('Cookie value #2: '+ Cookie.readCookie('Temp')); // outputs: nothing
    
    It doesn't work for any path that I specify (either global '/' or targeted '/folder1/folder2)

    I guess the problem is in the readCookie method, but I don't know what to do to make it work...

    One more thing, is this gonna happen the same for the secure argument too? I couldn't test for it...

    TIA
    Proud member of this community.
    ____________________________

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Where is the "Temp" folder? Does it have a folder with the test name in it? Have you set your computer's security permissions to allow writing to it?

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I run the website on my localhost server (asp.net, VS 2008) at home, not online.

    The "Temp" folder: ~/Temp

    I didn't set any permissions...but if I just use:
    Cookie.createCookie("Test"), it works...shouldn't it work the same accross this domain?
    Proud member of this community.
    ____________________________

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    It may be that you need to set write/read permissions to your browsers Temp folder and the sub folders.

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, I'll check that out right away. Thanks for the tip!

    EDIT: I checked and it uses the default settings from the web.config file which has set the authentication mode to "Windows" so it should've worked (?).
    I'm using IIS7 (which is very different from IIS6) and I can't find where I can set the permissions for read/write except a window that tells me that I can select the "Physical Path Credentials" [username, password for the user that should be impersonated when accessing the physical path for the virtual directory] at the application root level.

    I left it blank because I don't need to impersonate no one because I set in web.config that I use my Windows account (?)

    What's wrong with it?? uuggghh
    Proud member of this community.
    ____________________________

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I impersonated my self and it still doesn't work...
    Proud member of this community.
    ____________________________

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Sorry for the delay getting back.
    I have IE6, but I'll try the cookie script on my localhost server and try both ways and post back what I find out.

  8. #8
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheer!
    Proud member of this community.
    ____________________________

  9. #9
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    I created 4 files to test the cookie script:

    htdocs/ (read cookie)
    htdocs/one/ (create and read cookie)
    htdocs/one/inner/ (read cookie)
    htdocs/two/ (read cookike)

    Using the default with no optional args, the cookie was read in
    htdocs/one/
    htdocs/one/inner/

    Using "/" the cookie was read in all locations

    Using "/two/" (still created in htdocs/one/), the cookie was only read in
    htdocs/two/

    Note* I needed to use the closing slash. I tried the secure argument using "1" and Opera indicated the cookie was secure.

  10. #10
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooh, so it works!!

    thank you, thank you!!

    *thumbs up!

    ** I guess the problem was in my config settings...
    Proud member of this community.
    ____________________________


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
  •