为什么javascript可以从过期的cookie中读取?

时间:2018-05-17 04:09:42

标签: javascript cookies

<script type="text/javascript">
    $(document).ready(function () {
        var maintenanceEndDate = new Date(2018, 4, 2, 19, 0, 0);

        if (getCookie('remindMaintenanceAgain') == '') {
            setCookie('remindMaintenanceAgain', '1', maintenanceEndDate);
        }

        alert(getCookie('remindMaintenanceAgain'));
    });

    function setCookie(cname, cvalue, maintenanceEndDate) {
        var expires = "expires=" + maintenanceEndDate;
        document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    }

    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1);
            }
            if (c.indexOf(name) == 0) {
                return c.substring(name.length, c.length);
            }
        }
        return "";
    }
</script>

为什么当Cookie的过期日期时间已设置为2018年5月2日19:00时,上面代码中的alert仍显示1

为什么上面的代码可以从过期的cookie中读取?

0 个答案:

没有答案