我正在为欧盟用户设置一个弹出窗口。我只想显示给欧盟用户而不是其他用户。我该怎么办?
有一个开源库可以做到这一点 https://github.com/osano/cookieconsent
但是由于某些原因,它不起作用。
将为所有用户显示弹出窗口,而不仅仅是欧盟用户。
答案 0 :(得分:0)
这是基本的cookie弹出代码:
<div id="cookiescript_injected" style="bottom: 20px; left: 20px; background-color: rgb(17, 17, 17); z-index: 999999; opacity: 0.9; position: fixed; padding: 15px; width: 300px; font-size: 13px; font-weight: normal; text-align: left; color: rgb(255, 255, 255); font-family: Arial, sans-serif; line-height: 20px; letter-spacing: normal;"><h4 id="cookiescript_header" style="background-color: rgb(17, 17, 17); z-index: 999999; padding: 0px 0px 7px; text-align: center; color: rgb(255, 255, 255); font-family: Arial, sans-serif; display: block; font-size: 18px; margin: 0px;">This website uses cookies</h4>This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy.<br><div id="cookiescript_accept" style="border: 0px none; padding: 11px 20px; cursor: pointer; margin: 5px 5px 5px 0px; transition: all 0.25s ease 0s; background-color: rgb(91, 183, 91); color: rgb(255, 255, 255); display: inline-block;">I agree</div><div id="cookiescript_reject" style="border: 0px none; padding: 11px 20px; cursor: pointer; margin: 5px 0px; transition: all 0.25s ease 0s; background-color: rgb(183, 91, 91); color: rgb(255, 255, 255); display: inline-block; float: right;">I disagree</div><div id="cookiescript_readmore" style="border: 0px none; padding: 6px 10px; cursor: pointer; margin: 5px 0px; transition: all 0.25s ease 0s; background-color: rgb(105, 118, 119); color: rgb(255, 255, 255); display: block; text-align: center; clear: both;">Read more</div><div style="clear:both"></div><a href="cookie-script.com" target="_blank" id="cookiescript_link" style="display: block; text-decoration: none; color: rgb(255, 255, 255); float: right; font-size: 90%;">You are accepting coockies for www.yourwebsite.com</a><div id="cookiescript_pixel" style="width: 1px; height: 1px; float: left;"></div></div>
您可以这样添加它:
$.get("https://ipinfo.io", function(response) {
if(response.country == "AL" || "AD" || "AM" || "AT" || "BY" || "BE" || "BA" || "BG" || "CH" || "CY" || "CZ" || "DE" || "DK" || "EE" || "ES" || "FO" || "FI" || "FR" || "GB" || "GE" || "GI" || "GR" || "HU" || "HR" || "IE" || "IS" || "IT" || "LT" || "LU" || "LV" || "MC" || "MK" || "MT" || "NO" || "NL" || "PL" || "PT" || "RO" || "RU" || "SE" || "SI" || "SK" || "SM" || "TR" || "UA" || "VA"){
var para = document.createElement(" <div id="cookiescript_injected" style="bottom: 20px; left: 20px; background-color: rgb(17, 17, 17); z-index: 999999; opacity: 0.9; position: fixed; padding: 15px; width: 300px; font-size: 13px; font-weight: normal; text-align: left; color: rgb(255, 255, 255); font-family: Arial, sans-serif; line-height: 20px; letter-spacing: normal;"><h4 id="cookiescript_header" style="background-color: rgb(17, 17, 17); z-index: 999999; padding: 0px 0px 7px; text-align: center; color: rgb(255, 255, 255); font-family: Arial, sans-serif; display: block; font-size: 18px; margin: 0px;">This website uses cookies</h4>This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy.<br><div id="cookiescript_accept" style="border: 0px none; padding: 11px 20px; cursor: pointer; margin: 5px 5px 5px 0px; transition: all 0.25s ease 0s; background-color: rgb(91, 183, 91); color: rgb(255, 255, 255); display: inline-block;">I agree</div><div id="cookiescript_reject" style="border: 0px none; padding: 11px 20px; cursor: pointer; margin: 5px 0px; transition: all 0.25s ease 0s; background-color: rgb(183, 91, 91); color: rgb(255, 255, 255); display: inline-block; float: right;">I disagree</div><div id="cookiescript_readmore" style="border: 0px none; padding: 6px 10px; cursor: pointer; margin: 5px 0px; transition: all 0.25s ease 0s; background-color: rgb(105, 118, 119); color: rgb(255, 255, 255); display: block; text-align: center; clear: both;">Read more</div><div style="clear:both"></div><a href="cookie-script.com" target="_blank" id="cookiescript_link" style="display: block; text-decoration: none; color: rgb(255, 255, 255); float: right; font-size: 90%;">You are accepting coockies for www.yourwebsite.com</a><div id="cookiescript_pixel" style="width: 1px; height: 1px; float: left;"></div></div>");
var element = document.getElementById("div1");
element.appendChild(para);
}else{
//Country is not EU
}
console.log(response.city, response.country);
}, "jsonp");