Skip to content

A JavaScript code that provides a reliable way to check whether third-party cookies are supported in the user's web browser.

Notifications You must be signed in to change notification settings

VictorlBueno/Third-Party-Cookies-Check

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 

Repository files navigation

What Cookies Check does? 🍪✔️

This simple method verifies that the user's web browser supports and allows the use of cookies, allowing for a reliable experience when using content that requires cookies. By checking the browser's compatibility with cookies, this code prevents possible error messages or malfunctions. Its purpose is to reliably check if the browser allows cookies and create a conditional statement based on the result.

  • Checks if the user's browser supports cookies.
  • Handles browsers that don't support navigator.cookieEnabled.
  • Verifies if the browser allows third-party cookies, avoiding errors.
  • Provides a reliable mechanism for smooth user experience.
  • Enables displaying alternative content when third-party cookies are blocked.
  • Prevents user-facing errors due to cookie-related issues.

Case of use

Some browsers have settings to block third-party cookies, and if a user with this configuration accesses a website that has content requiring third-party cookies authorization, the content generates an error and is not displayed to the user. By using this code, it is possible to check if the user's browser allows cookies and create a conditional statement to show alternative content in case the browser blocks third-party cookies. This way, it prevents the user from encountering an error message or having a bad experience.

How to use

Suppose a website utilizes iframes to embed third-party content. Before loading and interacting with this content, the parent window wants to verify if cookies are enabled in the user's browser.

  1. In your main webpage, include the main page code to send a message to the iframe (other webpage) containing the support page code that needs to be deployed to another domain. Or you can just use the code on the main webpage that contains a link to point to www.victor.com.de/cookiescheck, a website that already has the support page code implemented.
  2. The iframe will process the message, perform the cookie test, and reply back to the parent window true or false.

  3. cookiescheck

  4. Based on the result, the parent window can make informed decisions about displaying certain content or enabling specific features that require cookies.
// Call the cookieTest function with the provided URL and a callback function.
// You can change this website to another one where you implemented the support page code (which needs to be at a different domain than the main page code).
cookieTest('https://victor.com.de/cookiescheck/', (result) => {
  // If cookies is accepted
  if(result) {
    // YOUR ORIGINAL CODE HERE
  } else {
    // YOUR ALTERNATIVE CODE HERE
  }
}

Conclusion

The JavaScript code provides a reliable way to check if cookies are supported in the user's web browser. By using the navigator.cookieEnabled property and setting a test cookie, the code ensures a comprehensive approach to determine cookie support.

This functionality is valuable for websites and web applications that depend on cookies for essential functionalities or personalized user experiences. By being aware of cookie support, developers can tailor their websites to provide the best possible user experience for all visitors.

Links     LinkedIn  •   Website  •   Instagram

About

A JavaScript code that provides a reliable way to check whether third-party cookies are supported in the user's web browser.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published