JavaScript 清除所有 cookie

标签 javascript html cookies

我正在尝试清除所有 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,它会被循环遍历,但不会被删除,并且它始终是数组中的第一项。任何帮助解决这个问题的帮助都将非常感激。

最佳答案

您是否尝试过将所有 cookie 时间格式设置为“一毫秒前”?这应该可以解决问题。

关于JavaScript 清除所有 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/876830/

相关文章:

javascript - 我应该将 javascript 函数放在 grails 中的什么位置才能访问它?

javascript - TypeScript 无法识别 $.ajax 调用

html - CSS Hover 在本地工作,但在开发服务器上不工作

javascript - 通过 JS 设置的 Cookie 在浏览器关闭时过期未过期

cookies - Node.js http.ClientRequest : get raw headers

php - 为什么在 CURLOPT_COOKIESSION 设置为默认值时需要 CURLOPT_COOKIEJAR

javascript for循环在html div中打印多行

javascript - 在 Javascript 中创建私有(private)静态函数?

javascript - 使用 fullpage.js 将类添加到粘性菜单

html - 水平对齐框