我正在为我的客户在网站上添加一项功能,如果禁用了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);
}
在铬土地上似乎都很好。接下来,我转到IE7,阻止所有cookie,为安全起见,删除我的所有历史记录和cookie以防万一。我希望看到我的div,但没有。
所以,我在if中添加了一个else(document.cookies ==“”){}在javascript中读取cookie,确定有我的测试cookie。
我进入了工具 - > Internnet选项 - >隐私标签 - >并将滑块一直移到顶部,“阻止所有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也支持它。
这件事真的开始让我紧张了!!!
答案 0 :(得分:0)
这是我发现的旧测试。
它仍适用于IE吗?
对于其他浏览器,它需要一个setCookie,getCookie和delCookie函数,如果需要我也可以使用它
function isCookieEnabled() {
if (document.all) return navigator.cookieEnabled;
setCookie('testcookie',today.getTime());
var tc = getCookie('testcookie');
delCookie('testcookie');
return (tc == today.getTime());
}
/* Cookie functions originally by Bill Dortsch */
function setCookie (name,value,expires,path,theDomain,secure) {
value = escape(value);
var theCookie = name + "=" + value +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((theDomain) ? "; domain=" + theDomain : "") +
((secure) ? "; secure" : "");
document.cookie = theCookie;
}
function getCookie(Name) {
var search = Name + "="
if (document.cookie.length > 0) { // if there are any cookies
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset)
// set index of end of cookie value
if (end == -1) end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
}
}
function delCookie(name,path,domain) {
if (getCookie(name)) document.cookie = name + "=" +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-70 00:00:01 GMT";
// alert(name+' marked for deletion');
}