Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryCookies and Browser Interaction

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Testing for Cookies Enabled

This article describes how to test if a site visitor's browser accepts cookies.

The code can be a solution if your site uses cookies to provide a better user experience. Or needs cookies for certain site features to work properly.

Using cookies for a better user experience could include:

  • Providing links to return to pages previously visited.

  • Providing links to pages similar to the ones already visited.

  • Tracking page visit sequence to determine how to improved the site.

Needing cookies for features to work properly could include:

  • A site shopping cart.

  • A membership area with cookie authentication.

  • A rating system.

Many browsers have cookies enabled. But some don't.

JavaScript can be used to test if a browser accepts cookies.

When the browser won't accept the test cookie, a message can be displayed to the user.

This is especially visitor-friendly for places that must use cookies, such as a member area. Present a "cookies must be enabled" message instead of denying the login again and again and raising the visitor's anxiety or disgust with the site.

A Browser Cookie Test

To test whether or not the visitor's browser is cookie-enabled, put this JavaScript somewhere into your web page. It can be in the HEAD area or the BODY.

<script type="text/javascript">
var CookieName = "TestCookie";
document.cookie = CookieName+"=yes;";
if(document.cookie.indexOf(CookieName) == -1) {
   alert("Cookies are required to use shopping cart.");
   }
</script>

The test cookie is temporary. It will be removed from the browser when the browser is closed.

The cookie name is "TestCookie", which may be changed. To change the test cookie name, change the content between the quotation marks on the line beginning with var CookieName =

For a live implementation, replace the alert message with something appropriate. Or, if you have JavaScript skills, replace the entire alert(...) line with whatever you want the JavaScript to do when the browser doesn't have cookies enabled.

Third-Party Cookies

Third-party cookies are cookies set by a domain other than the one in the browser's address bar.

Most browsers will accept cookies from the domain in the address bar but no third-party cookies. This is fast becoming the norm. In other words, third-party cookies are being deprecated but, probably for some time yet, they can be employed with user permission.

To test whether or not a visitor's browser accepts third-party cookies and to provide a notice to the visitor that a site feature needs third party cookies enabled, follow these steps:

  1. Put the JavaScript on a web page at the other domain, the domain that will set the third-party cookie.

  2. The page the browser will load do to the test contains an iframe for loading the cookie-testing JavaScript on the other domain. Example:

    <iframe 
       src="https://example.com/test.html"
       height="100" 
       width="100">
    </iframe>      
    
    

Loading the web page with the iframe tests the browser to determine whether or not it will accept the third-party cookie.

If your site works better with cookies or if it needs cookies for certain features to be available, use the cookie testing JavaScript to determine whether or not visitors' browsers accept cookies.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC