SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Need help while writing cooking in javascript :

    Need help while writing cooking in javascript :

    This will work fine
    document.cookie = 'mycookie=hello;expires=Fri, 17 Dec 2010 10:00:00 GMT';

    but if ; (semicolon) is appearing in value of cookie then it terminates the cookie for ex :

    document.cookie = 'mycookie=hello;after semicolon it wont work;expires=Fri, 17 Dec 2010 10:00:00 GMT';

    in above example you can see cookie value contains ; inside it after hello and I think this is creating problem

    what I am trying to do is as below :
    I am writing no. of values in one cookie
    Write cookie 1st time : o/p : name_1 | last_2 | dob_3
    Write cookie 2nd time with more values: o/p : name_1;name_2 | last_2;name_2 | dob_3;dob_3

    Now when I am writing cooking and in between this if ; (semicolon) appears in value of cookie then I think it is not writing cookie completely coz it is termination cookie when he finds 1st ; (semicolon)..

    can anyone help me how to write that ; inside cookie

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Try using these cookie functions. They'll make your life much easier:

    Code:
    function setCookie(name, value, expires, path, domain, secure)
    {
        var today = new Date();
        today.setTime(today.getTime());
        if (expires)
        {
            expires = expires * 1000 * 60 * 60 * 24;
        }
        var expires = new Date(today.getTime() + (expires));
    
        document.cookie= name + "=" + escape(value) +
            ((expires) ? "; expires=" + expires.toUTCString() : "") +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure" : "");
    }
    
    function getCookie(name)
    {
        var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1)
        {
            begin = dc.indexOf(prefix);
            if (begin != 0) return null;
        }
        else
        {
            begin += 2;
        }
        var end = document.cookie.indexOf(";", begin);
        if (end == -1)
            end = dc.length;
        return unescape(dc.substring(begin + prefix.length, end));
    }
    
    function deleteCookie(name, path, domain)
    {
        if (getCookie(name))
        {
            document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }
    }

  3. #3
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    let me give a try , i'll get back 2 u after that


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
  •