开发者

How to show a message only if cookies are disabled in browser? [duplicate]

This question already has answers here: Check if the client accepts cookie in javascript? (3 answers) Closed 9 years ago.

How to show a message only if cookies are disabled in browser? like http://stackoverflow.com show if JavaScript 开发者_开发知识库is disabled.


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;
}


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);
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜