【发布时间】:2016-09-21 14:47:11
【问题描述】:
我正在尝试清除我的所有 cookie,其中一些设置在服务器上,其中一些是浏览器 cookie。
我尝试过使用 document.cookie = "" 但这并不能满足我的需求,所以我编写了一些 Javascript 代码,如下所示
pauseClearAllCookies 函数被称为 onload。
<script type="text/javascript">
//var cookie_names = new Array("__utma", "__utmb", "__utmc", "__utmz", "mortgage", "track_source","currency","selenium_testing","visit_secure_token", "rdb_history", "_csuid", "search", "finance", "searchhistory");
function pauseClearAllCookies(){
Set_Cookie("selenium_testing","1");
drawTable();
setTimeout("checkCookies();",1000);
document.getElementById('msg').innerHTML = "Gathering Cookies....";
}
var cookie_counter = 0;
var cookieList;
function checkCookies(){
if(document.cookie.indexOf(";") != -1){
cookieList = document.cookie.split(";");
}else{
cookieList = [document.cookie];
}
setTimeout("clearAllCookies();",1000);
}
function clearAllCookies(){
document.getElementById('msg').innerHTML = "Deleting Cookies....";
if(cookie_counter < cookieList.length ){
var cookieName = "";
if(cookieList[cookie_counter].indexOf("=") != -1){
cookieName = cookieList[cookie_counter].split("=")[0];
}else{
cookieName = cookieList[cookie_counter];
}
document.getElementById('msg').innerHTML = "Deleting Cookie: "+cookieName;
// clear js cookies
Delete_Cookie(cookieName, '/', document.domain);
Delete_Cookie(cookieName, '/', '.www.abc.co.uk');
Delete_Cookie(cookieName, '/', 'www.abc.co.uk');
Delete_Cookie(cookieName, '/', '.abc.co.uk');
// clear server cookies
Delete_Cookie(cookieName, '/', '');
// increment counter
cookie_counter++;
drawTable();
//recall the function
setTimeout("clearAllCookies();",800);
}else{
Set_Cookie("selenium_testing","1");
}
}
function drawTable() {
var allcookies = document.cookie.split(";");
document.getElementById('heading').innerHTML = allcookies.length + " cookie found";
var table_html = "<table class='data'>";
for(var i=0; i < allcookies.length; i++){
var cookie = allcookies[i].split("=");
table_html += "<tr>";
table_html += "<td>"+(i+1)+"</td>";
table_html += "<td>" + cookie[0] + "</td>";
table_html += "<td>" + cookie[1] + "</td>";
table_html += "<tr>";
}
table_html += "</table>";
document.getElementById('table').innerHTML = table_html;
}
</script>
问题是,每次我运行此代码时,都会留下一个 cookie,它会循环通过但不会被删除,并且它始终在数组中排名第一。非常感谢您对解决此问题的任何帮助。
【问题讨论】:
-
BerggreenDK 有一个很好的观点。此外,您甚至没有显示您的 Delete_Cookie 功能...
-
cookie_counter 的值从何而来?这是完整的代码吗?
标签: javascript html cookies