Oct 5, 2009

Reading the cookies from the browser in javascript

Here objective is to read the cookies value in javascript. Here is the function which needs to placed inside the "script" of the javascript.This function will read the cookie value from the available cookies in the browser based on the key passed for the cookie.

Here the function is called on page load
-------------------------------------------------------------------


function OnLoadFuntion() {
        name = 'KeyName';
        readCookie(name);
        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) {
                  
                    if (c.substring(nameEQ.length, c.length) == 'TRUE') {
                        //Write functionality
                    }
                }
            }
            return null;
        }
    }
----------------------------------------------------------


So your job is done
Enjoy!!!

No comments:

Post a Comment