我正在尝试创建一个弹出窗口,提示网站访问者与支持人员交谈。但是,一旦用户看到弹出窗口,我想确保他们在访问网站的其他部分时不会再次获得主动弹出窗口。
为此,我尝试创建一个将在1天后过期的cookie,如果脚本可以从访问者检索该cookie,则隐藏弹出窗口。这是我到目前为止的代码。弹出窗口工作正常,但是当我第二次访问页面时,我无法看到cookie并隐藏弹出窗口。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dialogBox</title>
<link rel="stylesheet" type="text/css" href="css/jquery.dialogbox.css">
</head>
<body style="background:#eee">
<div id="dialog"></div>
<div id="message"></div>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="js/jquery.dialogBox.js"></script>
<script>
if (messageCookie.value==null) {
$(function(){
var dialog = (function(){
$('#dialog').dialogBox({
hasClose: true,
effect: 'fade',
hasBtn: true,
confirmValue: 'Chat Now',
hasMask: true,
confirm:function(){
//Put website alive chat here
window.open('https://a4.websitealive.com/840/visitor/window/?code_id=355&dl='+escape(document.location.href),'wsa_840_306','height=200,width=200')
},
title: '',
content: 'Click the button below to chat with an expert',
callback: function(){
console.log('loading over')
setCookie(messageCookie, 1);
}
})
})
setTimeout(dialog, 1000);
})
}
else {
//do nothing
}
</script>
<script type="text/javascript">
function setCookie(key, value) {
var expires = new Date();
expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000));
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}
</script>
</body>
</html>