lfischer

Otto - Population

May 4th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function (){
  2.     // Settings
  3.     var cookiename_cto = "cto"; //Name of the cookie
  4.     var splitRate_cto = 50; //Split rate between the 2 partners
  5.  
  6.     // Check if cookie already exits
  7.     var population_cto = '';
  8.     var ca = document.cookie.split(';');
  9.     for(var i=0; i<ca.length; i++) {
  10.         var c = ca[i];
  11.         while (c.charAt(0)==' ') c = c.substring(1);
  12.         if (c.indexOf(cookiename_cto + "=") == 0){
  13.             population_cto = c.substring(cookiename_cto.length, c.length);
  14.             population_cto = population_cto.split('=')[1]; // Returns A or B
  15.         }
  16.     }
  17.  
  18.     // Populate the cookie, as there is no pool present in the cookie
  19.     if(population_cto == '' || population_cto == undefined){ console.log("we generated again a population value");
  20.         population_cto = (((Math.random() * 100 | 0) + 1) <= splitRate_cto) ? "A" : "B";
  21.     }
  22.  
  23.     var date = new Date();
  24.     date.setTime(date.getTime()+(365*24*60*60*1000));
  25.     var expires = "; expires="+date.toGMTString();
  26.     var default_path = "; path=/"; // really important so the var will be reachable from any page of the website
  27.     document.cookie = cookiename_cto+"="+population_cto+expires+default_path;
  28.     return population_cto; // Returns A or B
  29. }
Advertisement
Add Comment
Please, Sign In to add comment