如何使用JavaScript检查cookie是否被阻止?
答案 0 :(得分:5)
写一个cookie,然后阅读它......
例如:
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'); }
调用函数checkSessionCookie();