如果存在cookie,请不要运行JavaScript代码 [英] If cookie exists, don't run the JavaScript code

查看:83
本文介绍了如果存在cookie,请不要运行JavaScript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码,它显示了一个令人讨厌的ext intent overlay,当我去后退按钮时,但我不希望它显示如果一个特定的cookie存在。这是我的退出按钮制作的cookie。

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:

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);
        }
    });
});

我想做一个if和else语句:如果cookiedontshow存在,请勿为我的退出意图叠加层运行代码。我尝试了一些内容,但无法使用。有没有人有任何想法如何得到它的工作。

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.

推荐答案

您必须检查cookie字符串中的子字符串< a href =https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf =nofollow> indexOf 方法。

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

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

这篇关于如果存在cookie,请不要运行JavaScript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆