How to show a message only if cookies are disabled in browser? like http://stackoverflow.com show if JavaScript is disabled.
-
see http://stackoverflow.com/questions/531393/how-to-detect-if-cookies-are-disabled-is-it-possible – lexu Jan 30 '10 at 08:53
4 Answers
There used to be a JavaScript navigator.cookieEnabled
interface, but today browsers have a much wider range of cookie controls than just ‘enabled’/‘disabled’, including session/persistent options, first-party/third-party, site-specific settings and P3P. So sniffing this property is of little use now.
No, the only reliable way to find out whether you can set a cookie is to try to set it, and see if it's still there. Another wrinkle is that whilst many browsers will downgrade a persistent cookie to a session cookie when the user's privacy controls don't allow them, IE will not.
If you try to set a persistent cookie in IE when they are disabled, the cookie will simply be thrown on the floor. This can catch you out if you use a simple session-cookie checker, find cookies are enabled, and then try to set a persistent cookie. And you can't get away with trying to set as a session cookie and a persistent cookie, because when you set a persistent cookie in IE with persistent cookies disabled, it will even delete the existing session cookie of the same name. Oh IE!
So if you need to set a persistent cookie but make do with session where persistent isn't available, you'd have to use this first to find out what you're allowed to do:
// Find out what cookies are supported. Returns:
// null - no cookies
// false - only session cookies are allowed
// true - session cookies and persistent cookies are allowed
// (though the persistent cookies might not actually be persistent, if the user has set
// them to expire on browser exit)
//
function getCookieSupport() {
var persist= true;
do {
var c= 'gCStest='+Math.floor(Math.random()*100000000);
document.cookie= persist? c+';expires=Tue, 01-Jan-2030 00:00:00 GMT' : c;
if (document.cookie.indexOf(c)!==-1) {
document.cookie= c+';expires=Sat, 01-Jan-2000 00:00:00 GMT';
return persist;
}
} while (!(persist= !persist));
return null;
}

- 528,062
- 107
- 651
- 834
-
Might help somebody: I used this function to only show a jquery #dialog-message (once) to browsers who support persistent cookies. After I check for support I do a document.write of the dialog div, then set a cookie that php looks for so the dialog popup doesn't repeatedly annoy people. – PJ Brunet Sep 03 '13 at 09:30
-
anyone have source for a potential improvement of navigator.cookieEnabled since 2010? – Bombinosh Feb 28 '15 at 17:16
Try this out:
function are_cookies_enabled()
{
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);
}

- 28,083
- 8
- 65
- 90
-
-
It should, the 2nd check (attempt to create cookie and see if you can retrieve the value you attempted to set) is meant for every browser. The first check is just a quicker check if that property is stored in the DOM. (supported in modern browsers) – Dominic Barnes Jan 30 '10 at 09:09
-
navigator.cookieEnabled always returns true for IE11 , is there any other way to do the same ?? I am trying this for some time but not able to find any solution – Trilok Pathak Apr 14 '16 at 12:24
stackoverflow.com uses <noscript>
tags to show a special page if JavaScript is disabled. There's nothing equivalent for cookies that's built into the language. Your best bet is probably to take a look at this thread on how to detect whether cookies are disabled.
First you test to see if cookies are enabled. If they are, you output a message. You can hide the message using CSS and then unhide after the test, but then the user will see it if they don't use CSS. You could have the element but with nothing inside, so that it won't display and then user innerHTML to enter the message after the test.
Assuming cookies have already been set (or attempted to be) use:
var cookieMessage = "You don't have cookies turned on!";
var cookieHTML = document.getElementById("cookiesOff");
function cookieMessage() {
if(document.cookie.length == 0) {
cookieHTML.innerHTML(cookieMessage);
}
}
And then have an HTML element like an h3
or p
with an id of cookiesOff
.

- 36,459
- 25
- 97
- 163