使用jquery删除所有cookie并设置新 [英] Delete ALL Cookies with jquery and set new
问题描述
我想做的是当用户访问页面test.html时,删除之前访问的网页中的Cookie,如test1.html,test2.html等,并设置新的Cookie。
What I'm trying to do is when user visits page test.html , to delete cookies from pages he previously visited, like test1.html ,test2.html etc. and set new cookie.
有一个更简单的方法来删除所有以前设置的cookie(我有100s的页面一次一个地声明)与jquery?
Is there an easier way to delete all previously set cookies at once (I have 100s of pages to declare one by one every time) with jquery?
我不知道任何其他方式,除了删除一个,然后设置新:
I don't know any other way except to delete one by one and then set new:
$.cookie('test1', 'test1', { expires: -1, path: '/' });//deleting cookies from test1.html
$.cookie('test2', 'test2', { expires: -1, path: '/' });//deleting cookies from test2.html
$.cookie('test', 'test', { expires: 30, path: '/' });//setting new cookies
感谢
推荐答案
遵循 jquery-cookie 规范:
1)你调用$ .cookie(),它应该返回当前页面上的所有cookie。
2)只是遍历和删除如下:
1) You call $.cookie() which should return all of the cookies on the current page.
2) Just iterate through and remove as below:
var cookies = $.cookie();
for(var cookie in cookies) {
$.removeCookie(cookie);
}
注意:删除Cookie时,必须通过路径,域和安全选项,除非您依赖的是默认选项。
Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie, unless you're relying on the default options that is.
这篇关于使用jquery删除所有cookie并设置新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!