The UX Files

Experiences on the Front Lines of User Interfaces and Web Development

Check if Cookies Are Enabled on Client Side

Using just Javascript (with the help of jQuery and the jQuery cookie plugin), the following bit of code will do the trick on checking whether the user’s browser accepts cookies or not. This is helpful for posting a warning message on a login screen or when adding things to a shopping cart, etc. Although using cookies is obviously the most common way to achieve authentication, it is not always possible. The user may have adjusted their settings so that their browser rejects cookies when using Free Mobile Broadband, for example. It is worth considering how much such settings will limit a user’s ability to access your site. Using the code below is a simple way of determining whether or not cookies are accepted on any user’s browser.
<script>
jQuery(document).ready(function()
{
var TEST_COOKIE = 'test_cookie';
jQuery.cookie( TEST_COOKIE, true );
if ( jQuery.cookie ( TEST_COOKIE ) )
{
jQuery.cookie( TEST_COOKIE, null ); // delete the cookie
alert( 'Good news, cookies are enabled.' );
}
else
{
alert( 'Cookies not enabled. Please enable and try again.' );
}
}
</script>

Need hosting? Hostgator has unlimited domains and 99.9% uptime guarantee: