分析JS代码以在客户端获取cookie

时间:2015-03-25 09:50:40

标签: javascript cookies analysis

我是JS的新手,我正在分析一个很长的程序代码。我无法理解这个功能,只是它试图从客户端获取cookie。有人能指出这里的功能吗?

function get_cookie(a) {
        var b = a + "=";
        var c = "";
        if (document.cookie.length > 0) {
             offset = document.cookie.indexOf(b);
             if (offset != -1) {
                 offset += b.length;
                 end = document.cookie.indexOf(";", offset);
                 if (end == -1) {
                     end = document.cookie.length;
                }
                c = unescape(document.cookie.substring(offset, end));
            }
        }
        return c;
    }

1 个答案:

答案 0 :(得分:1)

function get_cookie(a) {
        var b = a + "="; // Getting argument a and assigning it to var b appending =
        var c = ""; // defining variable c  
        if (document.cookie.length > 0) {  //checking cookie length in browser
             offset = document.cookie.indexOf(b);  // checking b exists or not
             if (offset != -1) { // if b exists 
                 offset += b.length; // getting no of string and assigning it to offset
                 end = document.cookie.indexOf(";", offset); //checking if ';' is present
                 if (end == -1) { // if ';' is not there in cookie string, 
                     end = document.cookie.length; - // cookie is not set, SO assigning length to the variable end
                }
                c = unescape(document.cookie.substring(offset, end)); // assigning those values to c
            }
        }
        return c; // returning new cookie.
    }