www.webdeveloper.com
Results 1 to 2 of 2

Thread: Secure flag - JavaScript Cookies

  1. #1
    Join Date
    May 2011
    Location
    Florida
    Posts
    6

    Cool Secure flag - JavaScript Cookies

    So in JavaScript its written that we can use the ";secure" flag to set HTTPS only cookies. However, I'm not on SSL when I'm running debug locally and I find the cookie is still being written. Is this a bug or something I'm doing wrong?

    Code:
    var expDate = new Date();
    expDate.setDate(expDate.getDate() + 365);
    document.cookie = "mainFrameSize=" + 
                escape(document.getElementById("mainFrame").cols) + 
                ";expires=" + expDate.toGMTString() + ";secure";
    Edit: Using IE 7 on WinXP
    Last edited by Chizl; 09-20-2011 at 08:54 AM.

  2. #2
    Join Date
    May 2011
    Location
    Florida
    Posts
    6
    My problem was it was writting secure, when I wasn't on a secure connection. Then my server side code couldn't delete the cookie, because it would only pull unsecure cookies.

    I ended up doing a quick check to see if connection was secure and if so then add the secure; else I skip it.

    This works for now, but I think there is a bug here.

    Code:
    var expDate = new Date();
    expDate.setDate(expDate.getDate() + 365);
    
    var exireDate = "expires=" + expDate.toGMTString() + ";"
    var framesize = "mainFrameSize=" + escape(document.getElementById("mainFrame").cols) + ";";
    var secureit = "secure;";		
    var allValues = framesize + exireDate;
    
    if(isSecure())
             allValues += secureit;
    
    document.cookie = allValues;
    
    function isSecure() 
    {    
             return window.location.protocol == 'https:'; 
    }
    Last edited by Chizl; 09-20-2011 at 10:23 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles