ie7 cookies not being blocked on my site
I am adding a feature to a website for my client, showing a div on login page if cookies are disabled. I first started testing this within chrome, turned off cookies, and after
if (document.cookies == "") {
// show div to tell users cookies are disabled on their machine.
}
everything works. Also in my codebehind on Page_Load i am attempting to set a cookie
protected void Page_Load(object sender, EventArgs e)
{
Response.Cookies["test"].Value = "test";
Response.Cookies["test"].Expires = DateTime.Now.AddDays(1);
}
All seems well in chrome land. Next, I go over to IE7, block all cookies, and for safety sake, delete all my history and cookies just in case. I expected to see my div but didn't.
So, I added an else to my if (document.cookies == "" ) { } read the cookie in javascript and sure enough there is my test cookie.
I went into Tools -> Internnet Options -> Privacy tab -> and moved the slider all the way to the top, 'Block All Cookies'. In the privacy tab, I clicked the 'Advanced' button and set for both first party cookies and third party cookies to prompt. I'm thinking that it should be blocked.
For example, as a test, I go to google.com in ie7, it alerts me if I want to allow or block two cookies from google.
Is there anything special I need to be doing to check for cookies disabled in ie7?
I have created a cookies.js file, for creating, reading, and deleting
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);
/开发者_StackOverflow中文版/ 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;
}
Just went ahead and downloaded jquery.cookie.js and in my function for checking if cookies eanbled, I have this:
function cookiesEnabled() {
var TEST_COOKIE = 'test_cookie';
$.cookie(TEST_COOKIE, true);
if ($.cookie(TEST_COOKIE)) {
$.cookie(TEST_COOKIE, null);
return true;
}
return false;
}
this is working in chrome and firefox, but not in ie7.
i also tried this:
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;
}
this did work. I think at one point or navigator.cookieEnabled was supported by ie, but it appears that chrome and firefox support it as well.
This thing is really starting to get on my nerves!!!
Here is an old test I found.
Does it still work for IE?
For other browsers it needs a setCookie, getCookie and delCookie function which I also have if needed
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');
}
精彩评论