如何在JavaScript中删除Cookie?

有时您会想要删除一个cookie,以便随后尝试读取该cookie不会返回任何内容。为此,您只需要将到期日期设置为过去的某个时间即可。

示例

请尝试以下示例。它说明了如何通过将cookie的过期日期设置为当前日期后一个月来删除它。

<html>
   <head>
      <script>
         <!--
            function WriteCookie() {
               var now = new Date();
               now.setMonth( now.getMonth() - 1 );
               cookievalue = escape(document.myform.customer.value) + ";"

               document.cookie="name=" + cookievalue;
               document.cookie = "expires=" + now.toUTCString() + ";"
               document.write("Setting Cookies : " + "name=" + cookievalue );
            }
         //-->
      </script>
   </head>
   <body>
      <form name="myform" action="">
         Enter name: <input type="text" name="customer"/>
         <input type="button" value="Set Cookie" onclick="WriteCookie()"/>
      </form>
   </body>
</html>