Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts

    Most efficient code to retrieve a cookie value?

    So I've been using the function:

    Code:
    function get_cookie(Name) { 
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { 
    offset += search.length
    end = document.cookie.indexOf(";", offset);
    if (end == -1) end = document.cookie.length;
    returnvalue=unescape(document.cookie.substring(offset, end))
    }
    }
    return returnvalue;
    }
    
    //ie: get_cookie("username")
    to retrieve the value of a cookie in a name/value pair. I'm looking to roll my own soon using regular expressions, since the above is too chunky IMO. But I'm posting to see if anyone else wants to take a stab first. Just from the look of it I'm thinking just one line of code using regular expressions should do it.

    The RE techniques used should be as cross browser/ OS functional as possible. I've seen RE that don't work in IE5, for example. So whoever's got some time on their hands...
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #2
    New Coder
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I didn't test this very thoroughly, but it worked for a quick test in Mozilla and IE5-6. sorry, those are the only browers I have.

    Code:
    function get_cookie(Name)
    {
       return (document.cookie.match(new RegExp(Name + "=([^;]+)"))) ? unescape(RegExp.$1) : "";
    }

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Mines much the same but also checks for end of string in the event of multiple name/value pairs and the last pair is not followed by a comma seperator... But I like jmccaskill's use of the ternary better...

    Code:
    <script type="text/javascript">
    document.cookie='username=willy,password=test'; alert(document.cookie);
      function getCookie(name){ 
        if(document.cookie && new RegExp('('+name+'=[a-z]+)(?=,|\$)','gi').test(document.cookie)){
           alert(RegExp.$1);
        }  else alert('Not Present');
      }
    </script>
    </head>
    
    <body onload="getCookie('password');getCookie('username')">
    ......Willy

  • #4
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Interesting codes! Gives me some inspiration and direction. I'm going to try and come up with my own and post it here, and also test out your versions later.
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.


  •  

    Posting Permissions

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