Javascript cookie delete

本秂侑毒 提交于 2019-12-31 07:15:10

问题


If I create a cookie in Javascript document.cookie = 'unseen' how do I delete it when I navigate away from this page? This is the only cookie I am creating on the page.


回答1:


Run this:

document.cookie = 'unseen=; expires=Thu, 01-Jan-70 00:00:01 GMT;';

You're not deleting it, but telling the browser it's expired so it'll delete it.




回答2:


Set it it to expire to a time in the past. Function from http://techpatterns.com/downloads/javascript_cookies.php

function Delete_Cookie( name, path, domain ) {
    if ( Get_Cookie( name ) ) document.cookie = name + "=" +
      ( ( path ) ? ";path=" + path : "") +
      ( ( domain ) ? ";domain=" + domain : "" ) +
      ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}



回答3:


delete document.cookie

anyways i'm not sure if this is the right way to deal with cookies.



来源:https://stackoverflow.com/questions/2471347/javascript-cookie-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!