Javascript getCookie optimized version
SurferIX
Status: New User - Welcome
Joined: 13 Feb 2008
Posts: 2
Location: France
Reply Quote
How about that :

:: Code ::
function getCookie(search_name) {
  // note: document.cookie only returns name=value, not the other components
  var tab_cookies = document.cookie.split( ';' );
  for ( i = 0; i < tab_cookies.length; i++ ) {
    // now we'll split apart each name=value pair
    var cookie_tmp = tab_cookies[i].split('=');
    // and trim left/right whitespace while we're at it
    var cookie_name = cookie_tmp[0].replace(/^\s+|\s+$/g, '');
    // if the extracted name matches passed search_name
    if (cookie_name==search_name) {
      // we need to handle case where cookie has no value but exists (no = sign, that is):
      if (cookie_tmp.length>1) {
        return unescape( cookie_tmp[1].replace(/^\s+|\s+$/g, '') );
      }
      // cookie is initialized but no value => result = null
      return null;
    }
  }
  return null;
}
[/code]
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 3895
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
what problem does it fix?

I'll take a look at the code, but what did you find to be the issue that needed fixing?
Back to top
SurferIX
Status: New User - Welcome
Joined: 13 Feb 2008
Posts: 2
Location: France
Reply Quote
It doesn't fix anything.
You may have done some tests and you had left some variables that were useless.
It's just an optimized version, where I've removed variables that were of no use in your code.
Because you've been helpful to me, I've done a lighter and more optimized version.
I hope you like it.

Regards,

Olivier Pons
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 3895
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
oh, ok, thanks, somebody a while ago proposed a patch that fixed an actual bug that had been in this code for probably 10 years, just wanted to know what level this was.

Thanks for taking a look at the code, I'll double check and confirm, then add the patches, thanks.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours