Check if cookies are enabled
A cookie, also known as a web cookie, browser cookie, and HTTP cookie, is a piece of text stored on a user's computer by their web browser. A cookie can be used for authentication, storing site preferences, shopping cart contents, the identifier for a server-based session, or anything else that can be accomplished through storing text data.
A cookie consists of one or more name-value pairs containing bits of information, which may be encrypted for information privacy and data security purposes. The cookie is sent as a field in the header of the HTTP response by a web server to a web browser and then sent back unchanged by the browser each time it accesses that server.
Before you can use cookies you need to determine if the client has not disabled the cookie functionality in his browser. The code below demonstrates how you can do this.
function areCookiesEnabled()
{
var cookieEnabled = (navigator.cookieEnabled) ? true : false;
if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
{
document.cookie="testcookie";
cookieEnabled =
(document.cookie.indexOf("testcookie") != -1) ? true : false;
}
return (cookieEnabled);
}
To check if cookies are enabled by JQuery you can use the code snippet below.
$.cookie('test_cookie', 'cookie_value', { path: '/' });
if ($.cookie('test_cookie') == 'cookie_value') {
// cookie worked, set/enable appropriate things
}
Tested in: firefox 3.6, internet explorer 9