Advertisement
Guest User

Portent, Inc. UK Cookie Law Solution: cookieConsent.js

a guest
Jun 21st, 2012
1,618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Script by Portent, an internet marketing agency. www.portent.com
  3. */
  4.  
  5. // change this variable to match your domain
  6. var sitesubdomain = "www";
  7. var sitedomain = "example-domain.com";
  8.  
  9. function cookieConsent(sGeobytesInternet,sGeobytesMapReference) {
  10.     if (! Get_Cookie( 'cookieConsent' ) == true ) {
  11.         if (typeof(sGeobytesInternet) == "undefined") {
  12.            // Something has gone wrong with the variables, so set them to some default value,
  13.            // maybe set a error flag to check for later on.
  14.            var sGeobytesInternet = "unknown";
  15.         }
  16.         if (typeof(sGeobytesMapReference) == "undefined") {
  17.             var sGeobytesMapReference = "unknown";
  18.         }
  19.         sGeobytesMapReference = sGeobytesMapReference.replace(/^ */g, '').replace(/ *$/g, '');
  20.         if (sGeobytesMapReference == "Europe") {
  21.             if (confirm("We need your consent to set browser cookies we use on this site. Press 'OK' to give your consent. For more information, please read our privacy policy.")) {
  22.                 // parameters for Set_Cookie: name, value, expires, path, domain, secure
  23.                 Set_Cookie( 'cookieConsent', true, '90', '/', sitedomain, '' );
  24.             } else {
  25.                 // parameters for Delete_Cookie: name, path, domain
  26.                 Delete_Cookie('cookieConsent', '/', sitedomain);
  27.                 window.location = "http://" + sitesubdomain + "." + sitedomain + "/cookie-consent.htm";
  28.             }
  29.         }
  30.     } else if ( Get_Cookie( 'cookieConsent') == false ) {
  31.         Delete_Cookie('cookieConsent', '/', sitedomain);
  32.         window.location = "http://" + sitesubdomain + "." + sitedomain + "/cookie-consent.htm";
  33.     }
  34. }
  35.  
  36. function acceptCookies() {
  37.     // parameters for Set_Cookie: name, value, expires, path, domain, secure
  38.     Set_Cookie( 'cookieConsent', true, '90', '/', sitedomain, '' );
  39.     alert("You have consented to allowing cookies. Click 'OK' to continue to the homepage.");
  40.     window.location = "http://" + sitesubdomain + "." + sitedomain + "/";
  41. }
  42.  
  43. function denyCookies() {
  44.     // parameters for Delete_Cookie: name, path, domain
  45.     Delete_Cookie('cookieConsent', '/', sitedomain);
  46.     alert("You have choosen to NOT consent to allowing cookies.");
  47.     window.location = "http://" + sitesubdomain + "." + sitedomain + "/cookie-consent.htm";
  48. }
  49.  
  50. function Set_Cookie( name, value, expires, path, domain, secure ) {
  51.     // set time, it's in milliseconds
  52.     var today = new Date();
  53.     today.setTime( today.getTime() );
  54.  
  55.     /*
  56.     if the expires variable is set, make the correct
  57.     expires time, the current script below will set
  58.     it for x number of days, to make it for hours,
  59.     delete * 24, for minutes, delete * 60 * 24
  60.     */
  61.     if ( expires )  {
  62.         expires = expires * 1000 * 60 * 60 * 24;
  63.     }
  64.     var expires_date = new Date( today.getTime() + (expires) );
  65.  
  66.     document.cookie = name + "=" +escape( value ) +
  67.     ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
  68.     ( ( path ) ? ";path=" + path : "" ) +
  69.     ( ( domain ) ? ";domain=" + domain : "" ) +
  70.     ( ( secure ) ? ";secure" : "" );
  71. }
  72.  
  73. function Get_Cookie( check_name ) {
  74.     // first we'll split this cookie up into name/value pairs
  75.     // note: document.cookie only returns name=value, not the other components
  76.     var a_all_cookies = document.cookie.split( ';' );
  77.     var a_temp_cookie = '';
  78.     var cookie_name = '';
  79.     var cookie_value = '';
  80.     var b_cookie_found = false; // set boolean t/f default f
  81.  
  82.     for ( i = 0; i < a_all_cookies.length; i++ ) {
  83.         // now we'll split apart each name=value pair
  84.         a_temp_cookie = a_all_cookies[i].split( '=' );
  85.  
  86.         // and trim left/right whitespace while we're at it
  87.         cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
  88.  
  89.         // if the extracted name matches passed check_name
  90.         if ( cookie_name == check_name ) {
  91.             b_cookie_found = true;
  92.             // we need to handle case where cookie has no value but exists (no = sign, that is):
  93.             if ( a_temp_cookie.length > 1 ) {
  94.                 cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
  95.             }
  96.             // note that in cases where cookie is initialized but no value, null is returned
  97.             return cookie_value;
  98.             break;
  99.         }
  100.         a_temp_cookie = null;
  101.         cookie_name = '';
  102.     }
  103.     if ( !b_cookie_found )  {
  104.         return null;
  105.     }
  106. }
  107.  
  108. // this deletes the cookie when called
  109. function Delete_Cookie( name, path, domain ) {
  110. if ( Get_Cookie( name ) ) document.cookie = name + "=" +
  111. ( ( path ) ? ";path=" + path : "") +
  112. ( ( domain ) ? ";domain=" + domain : "" ) +
  113. ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement