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

javascript - Cookie “CookieName” has been rejected because there is already an HTTP-Only cookie but script tried to store a new

programmeradmin2浏览0评论
 var CookieName = "TestCookie";
  document.cookie = "CookieName=Cheecker; path =/; httponly=false;samesite=None;secure=true;"
     alert(document.cookie);
  if (document.cookie.indexOf(CookieName) == -1) {
    console.log("Cookies are required to use shopping carts.");
  }

  if (document.cookie.indexOf(CookieName) != -1) {
    console.log(
      "Thank you for enabling Third-Party cookies we only using it for our shopping carts"
    );
  }

I want to check if Third-party cookies are enabled in the user browser

 var CookieName = "TestCookie";
  document.cookie = "CookieName=Cheecker; path =/; httponly=false;samesite=None;secure=true;"
     alert(document.cookie);
  if (document.cookie.indexOf(CookieName) == -1) {
    console.log("Cookies are required to use shopping carts.");
  }

  if (document.cookie.indexOf(CookieName) != -1) {
    console.log(
      "Thank you for enabling Third-Party cookies we only using it for our shopping carts"
    );
  }

I want to check if Third-party cookies are enabled in the user browser

Share asked Sep 29, 2020 at 8:50 Abu Dujana MahalailAbu Dujana Mahalail 1,9112 gold badges11 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You've got a few questions wrapped up here. I'll answer the one you implied with your title: why are you getting the 'Cookie "CookieName" has been rejected...' error?

There are two reasons, both of which can be confirmed on Mozilla's "Using HTTP Cookies" page, in the 'Creating Cookies' section:

First:
HttpOnly is a flag, not a variable. You have httponly=false; in your cookie setting call. It should just be HttpOnly;, and incidentally the same applies for Secure;. Example:

document.cookie = "CookieName=Cheecker; path =/; HttpOnly; samesite=None; Secure;"

Second:
HttpOnly is a setting that restricts cookies to HTTP calls only. They can't be accessed by JavaScript... and so they can't be set by JavaScript, either. From Mozilla's page:

Cookies created via JavaScript cannot include the HttpOnly flag.

So. I can't speak to how to figure out whether third-party cookies are set in the user's browser, but you'll resolve your error by removing the HttpOnly flag from your cookie creation call.

发布评论

评论列表(0)

  1. 暂无评论