xSketchy0 xSketchy0 - 2 months ago 22
HTML Question

If cookie exists, don't run the JavaScript code

I've got this code that shows an annoying ext intent overlay when going to the back button, but I don't want it to show up if a specific cookie exists. This is the cookie my exit button makes.

JavaScript:

function dontshow(){
days=30;
myDate = new Date();
myDate.setTime(myDate.getTime()+(days*24*60*60*1000));
document.cookie = 'dontshow=OK; expires=' + myDate.toGMTString();
}


This is the code which shows my exit intent overlay.

JavaScript:

$(document).ready(function() {
canRun = false;
waitPeriod = 1000;// waiting time
setTimeout(function() { canRun = true; }, waitPeriod);

$(document).on("mouseleave", function (event) {
if (!canRun) {
return false;
}
if (event.pageY < 0) {
$(".leavemodal").fadeIn(600);
}
});
});


I want to make an if and else statement saying: If the cookie "dontshow" exists, don't run the code for my exit intent overlay. I've tried some stuff but it's not working. Does anyone have any ideas on how to get it to work.

Answer

You have to check if there substring in cookie string by indexOf method.

if (document.cookie.indexOf("dontshow=OK") != -1) ...