【发布时间】:2011-09-26 03:33:03
【问题描述】:
我正在为我的客户的网站添加一项功能,如果禁用 cookie,则会在登录页面上显示一个 div。我首先开始在 chrome 中测试这个,关闭 cookie,然后
if (document.cookies == "") {
// show div to tell users cookies are disabled on their machine.
}
一切正常。同样在我的 Page_Load 代码隐藏中,我试图设置一个 cookie
protected void Page_Load(object sender, EventArgs e)
{
Response.Cookies["test"].Value = "test";
Response.Cookies["test"].Expires = DateTime.Now.AddDays(1);
}
在 chrome 的土地上一切似乎都很好。接下来,我转到 IE7,阻止所有 cookie,并且为了安全起见,删除我所有的历史记录和 cookie,以防万一。我希望看到我的 div,但没有。
所以,我在 if (document.cookies == "" ) { } 中添加了一个 else 来读取 javascript 中的 cookie,果然有我的测试 cookie。
我进入工具 -> 互联网选项 -> 隐私选项卡 -> 并将滑块一直移动到顶部,“阻止所有 Cookie”。在隐私选项卡中,我单击了“高级”按钮并设置了第一方 cookie 和第三方 cookie 来提示。我觉得应该屏蔽掉。
例如,作为测试,我在 ie7 中访问 google.com,它会提醒我是否要允许或阻止来自 google 的两个 cookie。
我需要做些什么来检查 ie7 中禁用的 cookie 吗?
我创建了一个 cookies.js 文件,用于创建、读取和删除
function createCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
// we're going to search for the name of the cookie, followed by an =. So create this new string and put it in nameEQ
var nameEQ = name + "=";
// split document.cookie on the semicolons. ca becomes an array containing all cookies that are set for this domain and path.
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
// set c to the cookie to be checked.
var c = ca[i];
// if the first character is a space, remove it by using the 'substring()' method. continue doing this until the first character is not a space.
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
// now string c begins with the name of the current cookie. if this is the name of the desired cookie, we've found what we are looking for.
// we now only need to return the value of the cookie, which is the part of c that comes after nameEQ. By
// returning this value we also end the function: mission accomplished.
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
// if after having gone through all cookies, we haven't found the name we are looking for, the cookie is not present, just return null.
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
function cookiesEnabled() {
if (document.cookies == "") {
return false;
}
return true;
}
刚刚下载了 jquery.cookie.js,在我的检查 cookie 是否启用的函数中,我有这个:
function cookiesEnabled() {
var TEST_COOKIE = 'test_cookie';
$.cookie(TEST_COOKIE, true);
if ($.cookie(TEST_COOKIE)) {
$.cookie(TEST_COOKIE, null);
return true;
}
return false;
}
这适用于 chrome 和 firefox,但不适用于 ie7。
我也试过这个:
function cookiesEnabled() {
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;
}
这确实有效。我认为在某一时刻或 navigator.cookieEnabled 由 ie 支持,但似乎 chrome 和 firefox 也支持它。
这件事真的开始让我紧张了!!!
【问题讨论】:
标签: javascript asp.net cookies internet-explorer-7