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

Javascript 分析JS代码以在客户端获取cookie,javascript,cookies,analysis,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

我是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;
    }

if(offset!=-1)
offset在cookie中存在“a=”时值为1,如果不存在则值为-1。那么是检查b是否存在。所以这是有意义的。那么你能给上述条件下一个合适的定义吗?是的。所以
offset!=-如果找到
b
,则1将计算为true。
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.
    }