12

Can someone tell me how can I detect if the prevent third party protection is enabled in Safari browser using JavaScript.

Basavaraj J
  • 139
  • 6

1 Answers1

0

You can check by doing a quick test with an iframe to see if you're able to set/read a cookie with a 3rd party domain.

For example:

<!DOCTYPE html>
<html>
<head>
  <script>
    function testThirdPartyCookies(callback) {
      let iframe = document.createElement('iframe');
      iframe.style.display = 'none';
      iframe.src = 'https://thirdparty.domain.com'; // Replace with a third-party domain

  iframe.onload = function () {
    try {
      // Attempt to set a test cookie on the third-party domain
      iframe.contentWindow.document.cookie = 'testcookie=1; path=/';

      // Check if the test cookie was set successfully
      let cookieEnabled = iframe.contentWindow.document.cookie.includes('testcookie=1');
      callback(cookieEnabled);
    } catch (error) {
      // An error occurred, which might indicate that third-party cookies are blocked
      callback(false);
    } finally {
      // Remove the iframe
Tim Baker
  • 79
  • 1
  • 12