Javascript 我的网站上没有阻止ie7 Cookie
我正在为我的客户向网站添加一项功能,如果禁用Cookie,则在登录页面上显示一个div。我首先开始在chrome中测试这个,关闭cookies,然后Javascript 我的网站上没有阻止ie7 Cookie,javascript,asp.net,cookies,internet-explorer-7,Javascript,Asp.net,Cookies,Internet Explorer 7,我正在为我的客户向网站添加一项功能,如果禁用Cookie,则在登录页面上显示一个div。我首先开始在chrome中测试这个,关闭cookies,然后 if (document.cookies == "") { // show div to tell users cookies are disabled on their machine. } 一切正常。此外,在我的代码隐藏页面上,我正在尝试设置cookie protected void Page_Load(object sender,
if (document.cookies == "") {
// show div to tell users cookies are disabled on their machine.
}
一切正常。此外,在我的代码隐藏页面上,我正在尝试设置cookie
protected void Page_Load(object sender, EventArgs e)
{
Response.Cookies["test"].Value = "test";
Response.Cookies["test"].Expires = DateTime.Now.AddDays(1);
}
铬地似乎一切顺利。接下来,我转到IE7,阻止所有cookies,为了安全起见,删除我的所有历史记录和cookies以防万一。我本想去看我的剧组,但没去
因此,我在if document.cookies=={}中添加了一个else,读取javascript中的cookie,并确保有我的测试cookie
我进入工具->互联网选项->隐私选项卡->并将滑块一直移动到顶部,“阻止所有cookie”。在“隐私”选项卡中,我单击了“高级”按钮,并将第一方cookie和第三方cookie设置为提示。我想应该封锁它
例如,作为测试,我在ie7中访问google.com,它会提醒我是否允许或阻止来自google的两个cookie
我需要做些什么来检查ie7中禁用的cookies吗
我已经创建了一个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;
}
这在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;
}
这确实奏效了。我认为ie曾一度支持navigator.cookieEnabled,但chrome和firefox似乎也支持它
这件事真的开始让我心烦意乱了 这是我发现的一个老测试 它对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');
}
如果你能提供setCookie、getCookie和delCookie,那就太好了。使用我的cookies.js文件,我试图创建一个cookie,并在ie7中用javascript读取它,所有cookie都被阻止,我能够读取我创建的cookie。有什么想法吗?没有。听起来很奇怪。它在网络服务器上,你是否清除缓存并关闭并打开IEi清除缓存,关闭并重新打开IE7。刚刚尝试了你的js代码,仍然不起作用。我将把我的东西部署到我的web服务器上并在那里尝试。这是必须的。不要依赖IE中从磁盘加载的文件中发生的任何事情
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');
}