jquery, delete cookies

守給你的承諾、 提交于 2019-11-26 07:28:57

问题


I want to use JQuery to delete cookies ; I tried this

$.cookie(\'name\', \'\', { expires: -1 });

Then I refresh the page and the cookie is still there :

alert(\'name:\' +$.cookie(\'name\'));

Why? Thanks


回答1:


To delete a cookie with JQuery, set the value to null:

$.cookie("name", null, { path: '/' });

Edit: The final solution was to explicitly specify the path property whenever accessing the cookie, because the OP accesses the cookie from multiple pages in different directories, and thus the default paths were different (this was not described in the original question). The solution was discovered in discussion below, which explains why this answer was accepted - despite not being correct.

For some versions jQ cookie the solution above will set the cookie to string null. Thus not removing the cookie. Use the code as suggested below instead.

$.removeCookie('the_cookie', { path: '/' });



回答2:


You can try this:

$.removeCookie('the_cookie', { path: '/' });

source: https://github.com/carhartl/jquery-cookie#readme




回答3:


You can also delete cookies without using jquery.cookie plugin:

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



回答4:


it is the problem of misunderstand of cookie. Browsers recognize cookie values for not just keys also compare the options path & domain. So Browsers recognize different value which cookie values that key is 'name' with server setting option(path='/'; domain='mydomain.com') and key is 'name' with no option.




回答5:


Try this

 $.cookie('_cookieName', null, { path: '/' });

The { path: '/' } do the job for you




回答6:


Worked for me only when path was set, i.e.:

$.cookie('name', null, {path:'/'})



回答7:


What you are doing is correct, the problem is somewhere else, e.g. the cookie is being set again somehow on refresh.



来源:https://stackoverflow.com/questions/3671659/jquery-delete-cookies

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