Wednesday, September 9, 2009

Read Cookie using Java Script

 

function ReadCookie(cookieName)

{

     var theCookie = "" + document.cookie;

    

     var ind = theCookie.indexOf(cookieName);

     if (ind == -1 || cookieName == "")

        return "";

       

     var ind1 = theCookie.indexOf(';', ind);

     if (ind1 == -1)

        ind1 = theCookie.length;

       

     var val = unescape(theCookie.substring(ind+cookieName.length+1,ind1));

     val = ReplaceAll(val, cookieName + "=" , "");

     val = ReplaceAll(val, ";" , "");

     return val;

}

 

function ReplaceAll(str, pcFrom, pcTo)

{    

    var i = str.indexOf(pcFrom);   

    var c = str;

   

    while (i > -1)

    {      

        c = c.replace(pcFrom, pcTo);           

        i = c.indexOf(pcFrom);     

    }

   

    return c;

   

}

No comments: