Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-05-2012, 06:47 PM   PM User | #1
bazz
Master Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 5,211
Thanks: 39
Thanked 201 Times in 197 Posts
bazz will become famous soon enoughbazz will become famous soon enough
setting a cookie issue

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&amp;size=website_graphics&amp;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();

  
  
  
  );
__________________
"The day you stop learning is the day you become obsolete"! - my late Dad.

Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
Useful MySQL resource
Useful MySQL link
bazz is offline   Reply With Quote
Users who have thanked bazz for this post:
Kenank (09-07-2012)
Old 09-05-2012, 08:37 PM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Try changing
document.cookie=c_name + "=" + c_value;

To
document.cookie=c_name + "=" + c_value + ";path=/";

Before testing, use the browser's cookie manager to delete all current cookies on your domain.
Logic Ali is offline   Reply With Quote
Old 09-05-2012, 08:50 PM   PM User | #3
bazz
Master Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 5,211
Thanks: 39
Thanked 201 Times in 197 Posts
bazz will become famous soon enoughbazz will become famous soon enough
Thank you Logic Ali.

I thought it was to do with the Path setting but hadn't found how to set it. Much appreciated.

bazz
__________________
"The day you stop learning is the day you become obsolete"! - my late Dad.

Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
Useful MySQL resource
Useful MySQL link
bazz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:14 AM.


Advertisement
Log in to turn off these ads.