Javascript清除所有cookie

时间:2009-05-18 09:15:52

标签: 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,但它会循环播放,但不会被删除,而且它的ALWAYS项目在数组中排名第一。任何帮助整理这个问题的确非常感谢。

3 个答案:

答案 0 :(得分:2)

您是否尝试将所有Cookie时间格式设置为“毫秒前”?应该清理它。

答案 1 :(得分:1)

添加https://github.com/carhartl/jquery-cookie库并执行此脚本

for(var in $ .cookie())$。removeCookie(it);

答案 2 :(得分:0)

数组中的第一个对象始终标记为0

.split()[0]