Clearing all cookies with JavaScript

后端 未结 18 1015
别那么骄傲
别那么骄傲 2020-11-22 05:55

How do you delete all the cookies for the current domain using JavaScript?

18条回答
  •  情歌与酒
    2020-11-22 06:28

    As far as I know there's no way to do a blanket delete of any cookie set on the domain. You can clear a cookie if you know the name and if the script is on the same domain as the cookie.

    You can set the value to empty and the expiration date to somewhere in the past:

    var mydate = new Date();
    mydate.setTime(mydate.getTime() - 1);
    document.cookie = "username=; expires=" + mydate.toGMTString(); 
    

    There's an excellent article here on manipulating cookies using javascript.

提交回复
热议问题