如果您已关闭页面中的某些元素,我将添加cookie以保存。当我关闭cookieconsent横幅时,这些cookie适用于工作,但是当我使用完全相同的代码来获取其他cookie时,则不起作用。这是javascript:
function GetCookie(name) {
var arg=name+"=";
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
while (i<clen) {
var j=i+alen;
if (document.cookie.substring(i,j)==arg)
return "here";
i=document.cookie.indexOf(" ",i)+1;
if (i==0) break;
}
return null;
}
function testFirstCookie(){
var offset = new Date().getTimezoneOffset();
if ((offset >= -180) && (offset <= 240)) { //Europe and America
var visit=GetCookie("cookieCompliancyAccepted");
if (visit==null){
$("#myCookieConsent").fadeIn(800); // Show warning
} else {
// Already accepted
}
}
var offset = new Date().getTimezoneOffset();
if ((offset >= -180) && (offset <= 240)) { //Europe and America
var visit=GetCookie("saveBannerClosed");
console.log(visit);
if (visit==null){
console.log("show");
$("#savebanner").show(); // Show banner
} else {
console.log("hide");
$("#savebanner").hide(); //hidden
}
}
}
$(document).ready(function(){
$("#cookieButton").click(function(){
console.log('Understood');
var expire=new Date();
expire=new Date(expire.getTime()+7776000000);
document.cookie="cookieCompliancyAccepted=here; expires="+expire+";path=/";
$("#myCookieConsent").hide(800);
});
$("#denySaveBanner").click(function(){
console.log('Save Banner Closed');
var expire=new Date();
expire=new Date(expire.getTime()+604800000);
document.cookie="saveBannerClosed=here; expires="+expire+";path=/";
$("#savebanner").hide();
});
testFirstCookie();
});
这是html:
<center>
<div id="savebanner" style="border:5px solid red; width:60%;">
<img
src="image/savebanner.jpg"
onclick='window.open("https://www.battleforthenet.com")'
width="100%"
/>
<br />
<button
style="background-color:red; color:white"
id="denySaveBanner"
>
No thanks, I want to pay more for a worse internet
</button>
<button
style="background-color:green; color:white"
onclick="window.open('https://battleforthenet.com');"
>
Show me how I can help
</button>
</div>
</center>
当您按下按钮时它会隐藏,但是在重新加载页面时它不会重新隐藏。我要放弃这个,因为我已经尝试了大约一个月来解决这个问题。谢谢。
答案 0 :(得分:0)
可以稍微整理一下getCookie方法吗?并且明确指出正确/错误的回报,而不是寻找null
吗?希望这将使跟踪变得更容易。
cookies = document.cookie.split("; ")
for (c of cookies) {
const kvPair = c.split("=")
if (kvPair[0] === name) {
return true
}
}
return false
答案 1 :(得分:0)