Get cookie by name

前端 未结 30 1381
野的像风
野的像风 2020-11-22 03:58

I have a getter to get the value from a cookie.

Now I have 2 cookies by the name shares= and by the name obligations= .

I want to

30条回答
  •  终归单人心
    2020-11-22 04:30

    Simple function for Get cookie with cookie name:

    function getCookie(cn) {
        var name = cn+"=";
        var allCookie = decodeURIComponent(document.cookie).split(';');
        var cval = [];
        for(var i=0; i < allCookie.length; i++) {
            if (allCookie[i].trim().indexOf(name) == 0) {
                cval = allCookie[i].trim().split("=");
            }   
        }
        return (cval.length > 0) ? cval[1] : "";
    }
    

提交回复
热议问题