最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove cookie problems JS-COOKIE - Stack Overflow

programmeradmin2浏览0评论

JS Cookies remove cookie, undefined

Hi Guys, I have trying to remove a cookie from a domain but I can't, the console return undefined.

When I log in the cookie is set up in .dev.books but after that the URL change to platform.dev.books and keep the same cookies. The name of the cookie I want to remove is bookId, here is my try:

Cookies.set('bookId', ' ');

But instead to change the cookie value, create a new one with the domain platform.dev.books

If I use

Cookie.remove('bookId') 

the console return undefined

Thanks, any help will be helpful

JS Cookies remove cookie, undefined

Hi Guys, I have trying to remove a cookie from a domain but I can't, the console return undefined.

When I log in the cookie is set up in .dev.books.com but after that the URL change to platform.dev.books.com and keep the same cookies. The name of the cookie I want to remove is bookId, here is my try:

Cookies.set('bookId', ' ');

But instead to change the cookie value, create a new one with the domain platform.dev.books.com

If I use

Cookie.remove('bookId') 

the console return undefined

Thanks, any help will be helpful

Share Improve this question asked Jun 30, 2016 at 5:09 rodbocrodboc 4891 gold badge8 silver badges19 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 8
Cookie.remove('bookId') 

^^This wont work. You always need to include the relative path of the current page. Like this:

Cookies.remove('name', { path: '/', domain: '.yourdomain.com' })

The '/' stands for the root page.

You can unset cookie and set the expire header time to a past date

document.cookie = "bookId=; expires=Thu, 01 Jan 1970 00:00:00 UTC";

It really depends on the environment, you may have to set the domain attribute that matches the cookie domain stored in the browser

Cookies.remove('name', { domain: 'subdomain.site.com' });

You may encounter mismatch domain if the website is hosted behind a reverse proxy or load-balancer.

Best way to be sure to check the http header with fiddler.

Delete a cookie valid to the path of the current page:

Cookies.set('name', 'value', { path: '' })
Cookies.remove('name') // fail!
Cookies.remove('name', { path: '' }) // removed!

IMPORTANT! When deleting a cookie and you're not relying on the default attributes, you must pass the exact same path and domain attributes that were used to set the cookie:

Cookies.remove('name', { path: '', domain: '.yourdomain.com' })

Note: Removing a nonexistent cookie neither raises any exception nor returns any value.

I'll advice you always try to go through the readme of any package you use, thank me latter, lol. https://www.npmjs.com/package/js-cookie

发布评论

评论列表(0)

  1. 暂无评论