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

Analyzing JS code to grab a cookie at client-side

本文关键字:客户端 获取 cookie JS 代码 分析      更新时间:2023-09-26

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