getCookieValue in JavaScript

Here is my version of “getCookieValue” in JavaScript:

function getCookieValue( name ) {
    var all_cookies = document.cookie.split( ';' );
    var cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    for ( i = 0; i < all_cookies.length; i++ ) {
        cookie = all_cookies[i].split( '=' );
        cookie_name = cookie[0].replace(/^\s+|\s+$/g, '');
        if ( cookie_name == name ) {
            cookie_value = unescape( cookie[1].replace(/^\s+|\s+$/g, '') );
            return cookie_value;
            break;
        }
        cookie = '';
        cookie_name = '';
    }
    return null;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s