SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Posts rarely lloydi's Avatar
    Join Date
    Jan 2002
    Location
    Swindon UK
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Something is 'undefined' and I need to identify it!

    I have some JavaScript which is used to read and write cookies. As part of the scrip, I have to dismiss some ASPsession parts that are reported. There's some splitting going on and eventually I get to the parts I need.

    However, if no cookie exists, one line of code reports that the value of something is undefined. I need to know how I can identify this scenario, and react accordingly.

    I've tried:

    if (strExistingCookie==undefined) {...}
    if (strExistingCookie=="undefined") {...}
    if (strExistingCookie.undefined==true) {...}
    if (strExistingCookie.undefined) {...}
    if (isObject(strExistingCookie)) {...}
    if (isNull(strExistingCookie)) {...}
    if (isEmpty(strExistingCookie)) {...}

    And none of them work. I must be confusing VBscript with JavaScript and all sorts.

    So, in a nutshell, how can I do this?!

    function write_cookie(newItem) {
    //get value of any existing cookie data held
    read_cookie();
    //make sure it's not empty
    >>>> need something like 'if cookie is undefined then cookie' = "", otherwise it writes the value undefined in to the cookie
    var expDate = new Date();
    expDate.setTime (expDate.getTime() + 1000 * 60 * 60 * 24 * 365);
    var strCookie = strExistingCookie + newItem + "; path=/; expires=" + expDate.toGMTString();
    // alert ("WRITING this cookie value: " + strCookie);
    document.cookie = strCookie;
    }


    Hope this is clear!

    Look forward to your help, folks
    Build Your Own Web Site the Right Way!
    A beginners' HTML/CSS book with web standards at its heart
    The Ultimate HTML Reference
    A complete reference, in glorious hardback

  2. #2
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Try this:

    if (typeof strExistingCookie == "undefined") {...}

    aDog

  3. #3
    Posts rarely lloydi's Avatar
    Join Date
    Jan 2002
    Location
    Swindon UK
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for this - funnily enough, I didn't try that exactly, but did try typeof some other way (again, slightly incorrectly).

    In the end I managed to test using if (strExistingCookie == null) and that seemed to work.

    Out of interest, is one way better (i.e., more foolproof) than another?

    Thanks v much
    Build Your Own Web Site the Right Way!
    A beginners' HTML/CSS book with web standards at its heart
    The Ultimate HTML Reference
    A complete reference, in glorious hardback


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
  •