使用onclick事件删除cookie

时间:2012-12-06 20:24:32

标签: javascript cookies

我的脚本使用JavaScript设置名为stop_mobi的cookie。我在用户点击带有onclick事件的链接时尝试删除cookie但是当我测试时,cookie不会被删除。任何指向我出错的地方都会非常感激。感谢。

<script type="text/javascript">
    // set stop_mobi cookie for test purposes


    function setCookie(cookieName, cookieValue, nDays) {
        var today = new Date();
        var expire = new Date();
        if (nDays == null || nDays == 0) nDays = 1;
        expire.setTime(today.getTime() + 3600000 * 24 * nDays);
        document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
    }
    setCookie("stop_mobi", "yes", "5");

    function deleteCookie(name) {
        document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
        alert('Cookie deleted');
    }
</script>

<a href="" onclick="deleteCookie('stop_mobi');">Delete cookie</a>

0 个答案:

没有答案