How can I check if third-party cookies are enabled in JavaScript and HTML?

I am trying to find out a way in javascript to check if browser has Third-party cookie enabled or not , following apporach i’ve used but still there is no luck .

There are two files 1) Indec.html which contains following code:

<!doctype html>
<html>
<head>
</head>
<body>
  <script>
    function cookieTest(iFrameUri, callBack) {
      function messageHandler(event) {
        // Check for trusted origins here
        const data = JSON.parse(event.data);
        callBack(data["result"]);
        window.removeEventListener("message", messageHandler);
        document.body.removeChild(frame);
      }
      window.addEventListener("message", messageHandler);
      var frame = document.createElement("iframe");
      frame.src = iFrameUri;
      frame.sandbox = "allow-scripts allow-same-origin";
      frame.style = `display:none`;
      frame.onload = function(e) {
        frame.contentWindow.postMessage(JSON.stringify({ test: "cookie" }), window.location.origin);
      };
      document.body.appendChild(frame);
    }

    // Call the cookieTest function
    cookieTest('iframe.html', function(result) {
      // Callback function to handle the result
      console.log('Cookies enabled:', result);
      // Display the result on the page
      var resultElement = document.getElementById('result');
      resultElement.textContent = 'Cookies enabled: ' + result;
    });
  </script>
  <h1>Cookie Test</h1>
  <div id="result"></div>
</body>
</html>

Another one is iframe.html which contains the following code:

<!doctype html>
<html>
<head>
</head>
<body>
  <script>
    const checkCookiesEnable = () => {
      let isCookieEnabled = (window.navigator.cookieEnabled) ? true : false;
      if (typeof window.navigator.cookieEnabled == "undefined" && !isCookieEnabled) {
        document.cookie = "testcookie";
        isCookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
      }

      return isCookieEnabled;
    }

    (function () {
      window.addEventListener('message', event => {
        try {
          let data = JSON.parse(event.data);
          if (data['test'] !== 'cookie')
            return;
          let result = checkCookiesEnable();
          parent.postMessage(JSON.stringify({ 'result': result }), event.origin);
        } catch (e) {
          console.error(e);
        }
      });
    })();
  </script>
</body>
</html>

Yet i am still getting third-party cookie enabled in incognito mode. Please help here.