cookies - pop-up using javascript with visits and sample rate not working -


i trying make pop-up shows up: after third visit, , 20% of time.

i have right now, , i'm not great @ javascript , need figuring out i'm doing wrong. appreciated

visits = getcookie('nvisits'); if (!visits) {   visits = 1 } if (visits == 3) {   deletecookie('nvisits')   if (rand(0, 100) <= 20) {     window.open("http://mypopup.html", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");   } else {     if (visits < 3) {       ++visits;       cookiedata = visits;       setcookie('nvisits', cookiedata, expdate)     }   } } 

in head of page set cookie:

    expdate = new date; // in following line, 180 means 180 days.  expdate.settime(expdate.gettime() + 180 * 24 * 60 * 60 * 1000); expdate.togmtstring();    function setcookie(name, value, expires, path, domain, secure){  document.cookie= name + "=" + escape(value) + ((expires) ? "; expires=" + expires.togmtstring() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); }  function getcookie(name){  var dc = document.cookie; var prefix = name + "="; var begin = dc.indexof("; " + prefix); if (begin == -1){ begin = dc.indexof(prefix); if (begin != 0) return null;} else{begin += 2;} var end = document.cookie.indexof(";", begin); if (end == -1){end = dc.length;} return unescape(dc.substring(begin + prefix.length, end)); }   function deletecookie(name, path, domain){  if (getcookie(name)){ document.cookie = name + "=" +  ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=thu, 01-jan-70 00:00:01 gmt";} } 

----update---
had - working - it's getting random 20% work...

<script> visits = getcookie('nvisits'); if (!visits){visits = 1}; if (visits == 3 ){deletecookie('nvisits') window.open("https://popup.html", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");   } if (visits < 3){++visits; cookiedata = visits; setcookie('nvisits',cookiedata,expdate) } </script> 

because couln't find definition your function rand, rewrote snipped.

const visitscookiename = 'nvisits'; let visits = getcookie(visitscookiename) || 0; if (visits === 3) {     deletecookie(visitscookiename)     if (math.floor(math.random() * 5)) return;      window.open("http://mypopup.html", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");     return; }  setcookie(visitscookiename, ++visits, expdate); 

explanation

the random logic i'm using here simple. first multiply random float 5 return float number between 0 , ~4.99. after need floored value of it. it's between 0 , 4. because 0 == false hit 20% of tries.

another problem in snipped on line 10 (if (visits < 3) {). never true because in upper statement check if if (visits == 3) { (line 5) , because didn't change value of visits can't else 3.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -