如何使用JavaScript检查cookie是否被阻止

时间:2021-09-14 07:28:55

How can I check if cookies are being blocked using JavaScript?

如何使用JavaScript检查cookie是否被阻止?

1 个解决方案

#1


5  

Write a cookie and then read it...

写一个cookie,然后阅读它......

for example:

    function readCookie(cname) {
        a = trim(document.cookie);
        res = '';
        while(a != '') {
            cookiename = a.substring(0,a.search('='));
            cookiewert = a.substring(a.search('=')+1,a.search(';'));
            if(cookiewert == '' || a.search(';')==-1) {
                cookiewert = a.substring(a.search('=')+1,a.length);
            } else {
                cookiewert = a.substring(a.search('=')+1,a.search(';'));
            }

        if(cname == trim(cookiename)){
            res = cookiewert;
            return (res);
        }
        i = a.search(';')+1;
        if(i == 0){i = a.length}
        a = a.substring(i,a.length);
     }
     return(res)
    }

    function checkSessionCookie() {
     //write cookie
     document.cookie = 'sessioncookieallowed=true;';
     var check = readCookie('sessioncookieallowed');
    }

call function checkSessionCookie();

调用函数checkSessionCookie();

#1


5  

Write a cookie and then read it...

写一个cookie,然后阅读它......

for example:

    function readCookie(cname) {
        a = trim(document.cookie);
        res = '';
        while(a != '') {
            cookiename = a.substring(0,a.search('='));
            cookiewert = a.substring(a.search('=')+1,a.search(';'));
            if(cookiewert == '' || a.search(';')==-1) {
                cookiewert = a.substring(a.search('=')+1,a.length);
            } else {
                cookiewert = a.substring(a.search('=')+1,a.search(';'));
            }

        if(cname == trim(cookiename)){
            res = cookiewert;
            return (res);
        }
        i = a.search(';')+1;
        if(i == 0){i = a.length}
        a = a.substring(i,a.length);
     }
     return(res)
    }

    function checkSessionCookie() {
     //write cookie
     document.cookie = 'sessioncookieallowed=true;';
     var check = readCookie('sessioncookieallowed');
    }

call function checkSessionCookie();

调用函数checkSessionCookie();