Vij,
I am actually using the following functions in an app that uses the cookie
plugin:

/**
 * @name     sterilizeQueryString
 * @type     function
 * @param     {String} input
 * @desc     Turns a query string into an object
 * @return     Object b
 */
sterilizeQueryString = function(input,splitter,pair){
    try{
        if(typeof input != 'string') return null;
        if(!splitter){
            splitter = "&";
        }
        if(!pair) pair ="=";
        var a = input.split(splitter), b = Array();
        for(var i=0; i < a.length; i++)    a[i] = a[i].split(pair);

        for(var i = 0; i < a.length; i++) b[a[i][0]] = a[i][1];

        return b;
    }
    catch(e){
        $.iLogger.log(e.name + ': ' + e.message, 'error',
'sterilizeQueryString();');
    };
};

objectToQueryString = function( a , joiner, pair) {
    try{
        var s = [];
        if(!pair) pair ="=";
        if((typeof a == "object")){
            for(var j in a){
                if(typeof a[j] == "object" && a[j]){
                    s.push(encodeURIComponent(j) + pair + "__" +
objectToQueryString(a[j],"**","##") +"__");
                }
                else{
                    s.push( encodeURIComponent(j) + pair +
encodeURIComponent( a[j] ) );
                }
            };
            return s.join(joiner);
        }
        else{
            return a;
        };
    }
    catch(e){
        $.iLogger.log(e.name + ': ' + e.message, 'error',
'objectToQueryString();');
    };
};

queryStringToObject = function(s){
    try{
        if(s && typeof s == "string"){
            s = sterilizeQueryString(s);
            for(var j in s){
                if(/__(.*)__/.test(s[j])){
                    s[j] = s[j].replace(/__/g,"");
                    s[j] = sterilizeQueryString(s[j],"**","##")
                };
            };
            return s;
        }
        else{
            return s;
        };
    }
    catch(e){
        $.iLogger.log(e.name + ': ' + e.message, 'error',
'queryStringToObject();');
    };
}


This is the function that is using the previous functions:

//  if $use is set to cookie, this variable will be used for the cookie name
var $cookieName = 'dummyData';

//  if $use is set to cookie, this variable will be used for the cookie
params
var $cookieParams = {expires: 7};
/**
 * @name getSetCookies
 * @example getSetCookies();
 * @param {Boolean} getOnly
 * @desc Purpose of this function is to set and get cookie data
 * @see objectToQueryString
 * @see queryStringToObject
 */
getSetCookies = function(getOnly){
    $.iLogger.log('getSetCookies();');
    try{
        //
        if(getOnly){
            var tempSiteData = $.cookie($cookieName);
            if(tempSiteData){
                $sitedata = queryStringToObject(tempSiteData);
            }
            else{
                $.cookie($cookieName, objectToQueryString($sitedata,"&"),
$cookieParams);
            }
        }
        else{
            $.cookie($cookieName, objectToQueryString($sitedata,"&"),
$cookieParams);
        };
    }
    catch(e){
        $.iLogger.log(e.name + ': ' + e.message, 'error',
'getSetCookies();');
    };
}; // end : getSetCookies

Don't have time to explain it right now (wanted to at least answer you), but
if you have questions, I can answer them later on today.

Ben

On 9/25/07, Potluri <[EMAIL PROTECTED]> wrote:
>
>
>
> I don't see this is a problem with cookie plugin in particular but this is
> problem with cookie itself.
>
> Suppose this is the array
> var arr = [];
> arr.push({name:"vj",rollNo:10});
> arr.push({name:"kr",rollNo:15});
>
> The size of arr is 2 before storing in cookie
>
> I tried to store this array object in a cookie as $.cookie("cookieName",
> arr), and then later when I tried to  access the length of array with
> cookie
> as var tempArr= $.cookie("cookieName")--which should give array object
> named
> "arr".
>
> and then I tried to alert length of array as alert(tempArr.length); I was
> expecting size of array to be 2 but surprisingly its returning 31. I
> printed
> the value of tempArr like alert("arr="+ tempArr+" len="+tempArr.length).
>
> it prints arr=[object Object],[object Object]  len=31.
> When you calculate the each character of [object Object],[object Object]
> it
> returns 31.
>
> Can any one of you come up with quick solution of how to store array of
> array objects in a cookie.
>
> It'll be greatful.
>
> Regards,
> Vijay
> --
> View this message in context:
> http://www.nabble.com/problem-with-cookie-plugin-when-trying-to-store-array-object-in-cookie-tf4516799s15494.html#a12883618
> Sent from the JQuery mailing list archive at Nabble.com.
>
>


-- 
Benjamin Sterling
http://www.KenzoMedia.com
http://www.KenzoHosting.com

Reply via email to