启用 jQuery 检测 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8112634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery detecting cookies enabled
提问by user1044304
I have a jQuery-based web app. My requirement is fairly simple: I want to use jQuery to find out if the user has enabled or disabled cookies in their web browser. I'm aware that there's a plugin available which can be used to create/retrieve/delete/update a cookie. But, is there a way jQuery can detect the user agent's settings?
我有一个基于 jQuery 的 Web 应用程序。我的要求相当简单:我想使用 jQuery 来确定用户是否在其 Web 浏览器中启用或禁用了 cookie。我知道有一个插件可用于创建/检索/删除/更新 cookie。但是,jQuery 有没有办法检测用户代理的设置?
回答by Sarfraz
You don't need jQuery for that, you can use vanilla Javascript:
你不需要 jQuery,你可以使用 vanilla Javascript:
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);
}
回答by Josh Schultz
Internet Explorer (version 10, at least) always returns true for navigator.cookieEnabled
. Here's a method to work around this:
Internet Explorer(至少版本 10)对于navigator.cookieEnabled
. 这是解决此问题的方法:
function areCookiesEnabled() {
var cookieEnabled = navigator.cookieEnabled;
// When cookieEnabled flag is present and false then cookies are disabled.
if (cookieEnabled === false) {
return false;
}
// try to set a test cookie if we can't see any cookies and we're using
// either a browser that doesn't support navigator.cookieEnabled
// or IE (which always returns true for navigator.cookieEnabled)
if (!document.cookie && (cookieEnabled === null || /*@cc_on!@*/false))
{
document.cookie = "testcookie=1";
if (!document.cookie) {
return false;
} else {
document.cookie = "testcookie=; expires=" + new Date(0).toUTCString();
}
}
return true;
}
回答by steveyang
回答by wayofthefuture
I like this 1 liner function:
我喜欢这个 1 班轮功能:
function cookiesEnabled() {
return $.cookie('check', 'valid', { expires: 1 }) && $.cookie('check') == 'valid';
}
回答by Mohamed Allal
Why are you testing with the property cookieEnabled
? just try to create a cookie and check if it's done. And in case cookies work, delete the test created cookie.
你为什么要测试这个属性cookieEnabled
?只需尝试创建一个 cookie 并检查它是否已完成。如果 cookie 有效,请删除测试创建的 cookie。
function are_cookies_enabled()
{
document.cookie="testcookie=valid";
cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
if(cookieEnabled){
document.cookie = "testcookie=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
}
return cookieEnabled;
}
回答by Ritchie
You can simply set a test_cookie when loading your page. then delete if it is set, else alert('please enable cookies...');
您可以在加载页面时简单地设置一个 test_cookie。如果设置了,则删除,否则 alert('请启用 cookie...');
<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>