我编写了一个基于权限的解决方法,但我仍然觉得它远非理想,因为我必须在用户点击时让他们获得权限,但总比没有好。
我仍然需要一种方法来阻止谷歌丢弃不需要的 cookie。在我看来,没有必要用这么多 cookie 或任何 cookie 来跟踪用户。
此代码使用户单击该链接,当他们单击该链接时,系统会询问他们是否要显示该按钮以及该决定的后果是什么。
我的示例代码:http: //jsfiddle.net/CADjN/3/
现场演示 https://www.ortho.nl/orthomoleculaire-bibliotheek/artikel/8091/Langer-leven-met-vitamine-D
<script type="text/javascript">
// Function to set the cookie plusone.
function setCookie()
{
domain = "www.mysite.com";
c_name="plusone";
value="yes";
var exdate=new Date();
exdate.setDate(exdate.getDate() + 365);
var c_value=escape(value) + "; expires="+exdate.toUTCString()+';domain='+domain+';path=/';
document.cookie=c_name + "=" + c_value;
}
// Function to see if our plusone cookie exists and has value yes
// Returns true when the cookie is set, false if isn't set.
function getCookie()
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x.indexOf("plusone") != -1)
{
if(unescape(y)=="yes")
{
return true;
}
else
{
return false;
}
}
}
}
// Load the plusone module but first ask permission
function loadplusone(thelink)
{
// Cookie hasn't been set
if(!getCookie())
{
// Get permission
if(window.confirm("If you wish to 'plusone' this page we need to load a special button from Google.com.\n\nWith this button Google will place some cookies on your computer. Google uses these cookies for statistics and maintaing your login status if you have logged in with Google.\n\nDo you consent to loading the button and the placement of cookies by Google?\n\nIf you agree this website will place a cookie with a year lifetime on your computer to remember this and the plusone button will be loaded on every page where the plusone button is present on this site."))
{
// set cookie, load button
setCookie();
var jsnode = document.createElement('script');
jsnode.setAttribute('type','text/javascript');
jsnode.setAttribute('src','https://apis.google.com/js/plusone.js');
document.getElementsByTagName('head')[0].appendChild(jsnode);
document.getElementById(thelink).innerHTML = '';
}
}
// cookie has already been set, just load button.
else
{
var jsnode = document.createElement('script');
jsnode.setAttribute('type','text/javascript');
jsnode.setAttribute('src','https://apis.google.com/js/plusone.js');
document.getElementsByTagName('head')[0].appendChild(jsnode);
document.getElementById(thelink).innerHTML = '';
}
}
</script>
<!-- Where the plusone button should go this piece of code should be placed -->
<a id="plus1" href="javascript:loadplusone('plus1')">Show Google +1</a><g:plusone></g:plusone>
<!-- This should be placed below the above code or in the onload event of the document -->
<script type="text/javascript">
if(getCookie())
{
var jsnode = document.createElement('script');
jsnode.setAttribute('type','text/javascript');
jsnode.setAttribute('src','https://apis.google.com/js/plusone.js');
document.getElementsByTagName('head')[0].appendChild(jsnode);
document.getElementById('plus1').innerHTML = '';
}
</script>