Hi,
JS is not my strong point do I ask for your help please.
I am trying to make my site comply with the new cookie legislation, which means showing a note on the page giving the user the option of looking at a cookie policy or clicking to allow cookies.
What I have found online is almost working however, it sets a new cookie for each page that is looked at and requires the permission to be set by the user on every page. I want the user to set the cookie (allowing the site to use cookies), once and for that permission to apply to all pages, for 365 days.
I also need the script to check for the cookie and if it exists, to update the date to 'today'.
here is the code. it stores the path from the address bar which I think is why the permission is being required for all pages.I can't see where it does that.
(The double \\ are there for the perl interpreter).
Code:
function getCookie(c_name)
{
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==c_name)
{
return unescape(y);
}
}
}
function displayNotification()
{
// this sets the page background to semi-transparent black should work with all browsers
var message = "<div id='cookiewarning' ><div style='z-index:9999; position:absolute;top:10px;left:0px;width:100%'>";
// center vert
message = message + "<div style='width:100%;margin:0 auto;padding:5px;background:#fff url(/cgi-bin/mask.pl?client=518&size=website_graphics&img=bg_40alphaWhite.png);border:1px solid #DDDDDD;text-align:center; color: #000'>";
// this is the message displayed to the user.
message = message + "This site uses Cookies. By continuing to use the site you are consenting to this.<a href=\'/cgi-bin/cookies_policy.pl\' style=\'text-decoration:none;color:#1a1a1a;font-weight:bold;\' target=\'_blank\'>Click here to find out more about Cookies and how we use them.</a> ";
// Displays the I agree/disagree buttons.
// Feel free to change the address of the I disagree redirection to either a non-cookie site or a Google or the ICO web site
message = message + "<INPUT TYPE='button' VALUE='I do agree' onClick='JavaScript:setCookie(\\"jsCookieCheck\\",null,365);' style='background:#CC0000;color:#FFFFFF;padding:4px;border:0px;'/>";
// and this closes everything off.
message = message + "</div></div></div>";
document.writeln(message);
}
function setCookie(c_name,value,exdays)
{
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;
// set cookiewarning to hidden.
var cw = document.getElementById("cookiewarning");
cw.innerHTML = "";
}
function checkCookie()
{
var cookieName="jsCookieCheck";
var cookieChk=getCookie(cookieName);
if (cookieChk!=null && cookieChk!="")
{
// the jsCookieCheck cookie exists so we can assume the person has read the notification
// within the last year
setCookie(cookieName,cookieChk,365); // set the cookie to expire in a year.
}
else
{
// No cookie exists, so display the lightbox effect notification.
displayNotification();
}
}
checkCookie();
);