Saturday, May 30, 2009

Check if cookies are enabled on client side

Using just Javascript (with the help of jQuery and the jQuery cookie plugin), this 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.

<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 );
alert( 'Good news, cookies are enabled.' );
}
else
{
alert( 'Cookies are not enabled. Please enable and try again.' );
}
}
</script>

0 comments:

Post a Comment