简体   繁体   English

即使存在Cookie,也找不到Cookie

[英]Cookie not found even though it exists

I'm having a really strange problem and I'm looking for any possible ideas. 我有一个非常奇怪的问题,我正在寻找任何可能的想法。 I have a flyover that I load based on whether or not a cookie is found on the client's machine. 我根据是否在客户端的计算机上找到了cookie来加载天桥。 In the flyover there is a 'No thanks' checkbox saying " Don't show again ". 在天桥上有一个“不,谢谢”复选框,上面写着“ 不再显示 ”。 I check to see if it has been checked like this in the flyover page: 我检查是否在天桥页面中检查过这样:

$(document).ready(function() {
jQuery(window).bind("beforeunload", function(){ setCookieFO('noShowMerkleCpn','true',180);       });
}); 

function setCookieFO(c_name,value,exdays){
if($('#noThanks').attr('checked') ){
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}else{
}
} 

I have ran the code in Firebug and verified that the cookie gets saved on document unload. 我在Firebug中运行了代码并验证了cookie在文档卸载时保存了。 I can even go into my cookies and find the cookie. 我甚至可以进入我的cookie并找到cookie。 Here is how I check for the cookie: 以下是我检查cookie的方法:

function runFancyBox(){
var idx = document.cookie.indexOf('noShowMerkleCpn');
if(idx < 0 ){
$('#cpnForm').click();
}else{
}
} 

I don't really care about the cookie value. 我真的不关心cookie的价值。 I just check to see if it exists and display the flyover if it doesn't. 我只是检查它是否存在并显示天桥如果没有。 However, for some reason this check will return -1 even when the cookie exists. 但是,由于某种原因,即使cookie存在,此检查也将返回-1。

Extra info : 额外信息

The cookie is saved in my flyover.Html page under 该cookie保存在我的flyover.Html页面下

server/bank/ima

The script that looks for the cookie is on the same server 查找cookie的脚本位于同一服务器上

server/bank/ima/script

The cookie is saved with this Path value with Host: server 使用Host: server以此Path值保存cookie

/bank/ima/

Is there anything that jumps out that could be causing this issue? 是否有任何可能导致此问题的跳出来? Any suggestions? 有什么建议么?

two things i would check. 我会检查两件事。 first, you are setting document.cookie without appending, so you wipe out all previous cookies (bad idea). 首先,你没有附加设置document.cookie,所以你删除所有以前的cookie(坏主意)。 second, make sure to set the cookie's domain and path to the same domain as the page reading it. 第二,确保将cookie的域和路径设置为与读取它的页面相同的域。

also be aware of your usage of the checked attribute: 还要注意您对checked属性的使用:

if($('#noThanks').prop('checked'))

http://timmywillison.com/2011/When-to-use-.attr%28%29-and-.prop%28%29.html http://timmywillison.com/2011/When-to-use-.attr%28%29-and-.prop%28%29.html

I tried your code on both localhost and my server, and it works fine I assume you were careful to close your browser so that the cookie is actually set. 我在localhost和我的服务器上尝试了你的代码,它工作正常我假设你小心关闭浏览器,以便实际设置cookie。 My guess is you have a path problem, although from what you say about paths, it is certainly not obvious what it is. 我的猜测是你有一个路径问题,虽然从你对路径的看法来看,它肯定不明显是什么。

I saved the code on my server, so you can at least examine code that works. 我将代码保存在我的服务器上,因此您至少可以检查有效的代码。

The url to set the cookie is: http://www.bridgesights.com/hondobridge/bbohondo/setcookietest.php 设置cookie的网址是: http//www.bridgesights.com/hondobridge/bbohondo/setcookietest.php

The url to test the cookie is: http://www.bridgesights.com/hondobridge/bbohondo/bbohondo_files/getcookietest.php 测试cookie的网址是: http//www.bridgesights.com/hondobridge/bbohondo/bbohondo_files/getcookietest.php

Although this has nothing to do with your problem, I highly recommend the jquery-cookie plugin for manipulating cookies. 虽然这与您的问题无关,但我强烈建议使用jquery-cookie插件来操作cookie。 It is very lightweight and easy to use. 它非常轻巧,易于使用。 The link is: https://github.com/carhartl/jquery-cookie 链接是: https//github.com/carhartl/jquery-cookie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM