xSketchy0 xSketchy0 - 1 month ago 12x
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.


function dontshow(){
myDate = new Date();
document.cookie = 'dontshow=OK; expires=' + myDate.toGMTString();

This is the code which shows my exit intent overlay.


$(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) {

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.


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

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