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

I am trying to make a pop-up that only shows up:
after the third visit, and only 20% of the time.
I have this right now, and I'm not really great at Javascript and need some help figuring out exactly what I'm doing wrong. Any help would be 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 the head of the page I set the cookie:
expDate = new Date;
// in the 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---
I originally had this - which was working - it's just getting the random 20% to 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 I couln't find the definition for your function rand, I completely rewrote the 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 is really simple. First I multiply the random float to 5 which return a float number between 0 and ~4.99. After that we need the floored value of it. So it's between 0 and 4. Because 0 == false this hit only 20% of all tries.
Another problem in your snipped is on line 10 (if (visits < 3) {). This will never be true because in the upper statement you check if if (visits == 3) { (line 5) and because you didn't change the value of visits it can't be anything else but exactly 3.

Related

Redirect the returning site visitor to another URL

The Logic: Someone open my LP (landing page) first time > he closes my LP and go away > he open my LP second time.
So, how can i redirect him to another URL when he opens my Landing page second time?
P.S. I'm a newbie, so better for me is using cookies.
I have been searching stackoverflow and did not find an answer for exactly my question
function setCookie(cookie_name, value, days) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + days);
var cookie_value = escape(value) + ((days == null) ? '' : ';
expires=' + exdate.toUTCString());
document.cookie = cookie_name + '=' + cookie_value;
}
function getCookie(cookie_name) {
var x, y;
var val = document.cookie.split(';');
for (var i = 0; i < val.length; i++) {
x = val[i].substr(0, val[i].indexOf('='));
y = val[i].substr(val[i].indexOf('=') + 1);
x = x.replace(/^\s+|\s+$/g, '');
if (x == cookie_name) {
return unescape(y);
}
}
}
if (getCookie("isVisited") === "true") {
window.location.href = "https://redirect-url";
}

Expiration date on javascript cookie is not working -updated

UPDATE - made with suggestions from below:
I have the following script that pops up a window, after 3 visits, and to only 20% of visitors. The expiration date is set and shows in Developer tools for expiring in 180 days, but it's still popping up. What am I doing wrong? Any help is appreciated, I do not know javascript at all very well. This has to be done all in plain javascript - no jQuery
//setting the expiration date to 6 months from now//
expDate = new Date;
// in the following line, 180 means 180 days.//
expDate.setTime(expDate.getTime() + 180 * 24 * 60 * 60 * 1000);
expDate.toGMTString();
//making the cookie//
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=/wmb" + path : "") +
((domain) ? "; domain=www.example.com" + 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 += 1;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
//cookie name is called CustomSurvey//
visits = getCookie('CustomSurvey');
if (!visits) {
visits = 1
};
if (visits < 4)
//Math.random is set so that 20% of those visits actually get the popup//
if ((Math.random() * (100 - 1) + 1) < 20) {
//get location of where this popup came from; also contains variable from SM that will trickle down into reporting to tell where it came from//
var currentLocation = window.location;
window.open("https:mysurvey.com" + "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
}
if (visits < 3) {
++visits;
cookieData = visits;
setCookie('CustomSurvey', cookieData, expDate)
}
I changed to what you suggested (except the url - that part is just made up (the real one pops up). I did everything suggested except when I changed the visits > 3 , now nothing pops up (after clearing cache and clicking about 50 times.
This is what I have now:
//setting the expiration date to 6 months from now//
expDate = new Date;
// in the following line, 180 means 180 days.//
expDate.setDate(expDate.getDate() + 180)
//making the cookie//
function setCookie(name, value, expires, path, domain, secure){
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=/wmb" + path : "") +
((domain) ? "; domain=ptoweb.uspto.gov" + 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 += 1;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
//cookie name is called CustomSurvey//
visits = getCookie('CustomSurvey');
if (!visits) {
visits = 1
};
if (visits > 3)
//Math.random is set so that 20% of those visits actually get the popup//
if ((Math.random() * (100 - 1) + 1) < 20) {
//get location of where this popup came from; also contains variable from SM that will trickle down into reporting to tell where it came from//
var currentLocation = window.location;
window.open("https://www.surveymonkey" + "?" + currentLocation, "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
}
if (visits < 3) {
++visits;
cookieData = visits;
setCookie('CustomSurvey', cookieData, expDate)
}

Javascript Countdown timer function not working properly

I have developed a countdown timer function.
This function works fine. But the problem is it goes through the minus value too. So I want to stop the counting when its come to the 00:00:00.
How can I do this.please help me?
Javascript
function initCountdown() {
if( seconds == 0 && minutes == 0 && hours == 0 ){
clearInterval( interval ); }
if (seconds < 10) {
outputElement.innerHTML = hours + ": " + minutes + ": " + "0" + seconds + " ";
} else {
outputElement.innerHTML = hours + ": " + minutes + ": " + seconds + " "; }}
function count(){
time[2]--;
if (time[2] == -1) {
time[1]--;
time[2] = 59
}
if (time[1] == -1) {
time[0]--;
time[1] = 59
}
print();
}
var outputElement = document.getElementById('demo');
var time = document.getElementById("picker-dates1").value;
time = time.split(':'); }
HTML
<input type = "text" id = "picker-dates1"/>
<P id="demo"></p>
Replace print(); with if (time[0] >= 0) { print(); }

6 month expiration date on a javascript cookie

I'm not great with javascript, but I have one that is working, but it seems like it's not following the 6 month expiration date that I set. Can someone help troubleshoot? This is what I have when setting the cookie's expiration date:
expDate = new Date;
// in the 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){........
Any help troubleshooting would be perfect and very appreciated!
UPDATE - here is the script in its entirety. I'm calling a pop-up window only to show if it's been 3 page visits, and 20%, and 6 months expiration date. Just piecing it together so take it easy on me!!
expDate = new Date;
// in the 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));
}
visits = getCookie('nVisits');
if (!visits) {
visits = 1
};
if (visits == 3)
if ((Math.random() * (100 - 1) + 1) < 20)
{
window.open("https://mypoup.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)
}
Kaddath's comment solved the problem:
it seems that toGMTString() is obsolete and should not be used anymore, use toUTCString() instead.

javascript getting setting deleteing cookies

I am having trouble setting and deleting cookies in php Ive got no problem.. in javascript eh.. lots I know this question is everywhere but all the answers I come across seem to bring me the same fate so I have no idea whats going on.. this is what I have currently.
SetCookie("username",ztsUser,14,'/', '');
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" : "");
}
the error:
expires.toGMTString is not a function
The whole lot of cookie functions I have.. is:
var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) { endstr = document.cookie.length; }
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
return getCookieVal (j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
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";
}
}
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" : "");
}
if anyone knows a better method Im all ears.. but this is what I got at the moment, I need to set, update, delete, get the value of.. cookies
toGMTString has been deprecated. Try toUTCString instead:
function SetCookie (name,value,expires,path,domain,secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires.toUTCString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
You're also passing a number (14) when the function expects a Date (expires.toUTCString):
SetCookie("username", ztsUser, 14, '/', '');
What is 14 intended to be? 14 days? Assuming that, you can add this at the top of the function to support both numbers and dates:
if ('number' === typeof expires) {
expires = new Date(new Date().getTime() + expires * 86400000);
}
86400000 is the number of milliseconds in a day -- 24 * 60 * 60 * 1000.
There are already a number of great libraries for handling cookies in JS. Why not use one of them? Just go to your favorite search engine and enter "javascript cookie library" to find one.

Categories

Resources