YUI, Augmenting Cookie

Over the past week, I have had the opportunity to explore the YUI Cookie object. It has all the functionality from the 2007 article on Cookies, with the addition of sub cookies. Sub-cookies get around the maximum number of available cookie limitation, by allow the developer to store multiple cookies inside of one cookie. This is especially useful for sub-domains, see YUI -Cookie for more information.

However, there are a few methods that were missing: getNumberOfCookies, getCookieSize, and isCookiesEnabled. The getNumberOfCookies function returns the number of cookies currently set; simply splitting around ; seems to work in the browsers I tested. Let me know if there is a better way or browser issues I missed. The getCookieSize method does a pretty good estimation of the cookie size, by assuming all alpha-numeric characters are not escaped and thereby stored as 1 byte and all non-alpha-numeric characters are escaped and thereby stored as 3 bytes. Although, the later is not always true, it is a fairly accurate assumption; I am open to a better regex that considers the other characters which are not escaped. Lastly, the isCookiesEnabled enabled function determines if Cookies are enabled, first by looking at the navigator object, then by checking if there is a cookie set, and lastly by adding a test cookie.

Example 1: Augmenting Methods

(function() {
    var _YC = YAHOO.util.Cookie,
        _OBJ = {

         * Returns the number of cookies currently used.
         * @method getNumberOfCookies
         * @return {Number} The number of cookies.
         * @private
        getNumberOfCookies: function() {
            return ( + document.cookie).split(;).length;

         * Estimates the size of the cookie using 1 byte for each alpha-numeric character and 3 for each non-alpha-numeric character.
         * @method getCookieSize
         * @param key {String} Required. The cookie key to test.
         * @return {Number} The estimated cookie size.
         * @private
// note: this method has been improved, see Improved Cookie Size Calculation
        getCookieSize: function(key) {
            var str = ( + _YC.get(key)),
                strAlphaNum = str.replace(/[\W+]/g, );

            return strAlphaNum.length + ((str.length - strAlphaNum.length) * _SPECIAL_CHAR_BYTE_SIZE);

         * Tests if cookies are enabled.
         * @method isCookiesEnabled
         * @return {Boolean} True when cookies enabled;
         * @private
        isCookiesEnabled: function() {
            var testName = YAHOO.util.Cookie,
                testValue = test;

            if (navigator && ! navigator.cookieEnabled) {return false;} // navigator tells us no
            if (( + document.cookie).length) {return true;} // cookies are avialable, assume enabled

            _YC.set(testName, testValue);

            if (testValue === _YC.get(testName)) {
                return true;
            else {
                return false;

    YAHOO.lang.augmentObject(_YC, _OBJ);

With these methods included in addition to YUI, you can determine if you should use cookies to store user information (using isCookiesEnabled) and parse large data between multiple cookies (using getNumberOfCookies and getCookieSize). The worst case scenario will be that each cookie can not be larger than 4000 bytes and that you can not have more than 20 cookies per domain.