嗨,我有一个像下面的代码,我想在网站上显示覆盖层。这应该自动执行一次。
<html>
<head>
<style>
#boxoverlay{
position:fixed;
top:0px;
left:0px;
width:100%;
height:100%;
background:rgba(255,255,255,0.8);
z-index:555;
display:none;
}
</style>
</head>
<body>
<div id='boxoverlay'></div>
</body>
<script type = "text/javascript">
var popupState=document.cookie.indexOf('name=');
if(popupState!=0){
document.cookie = "name=opened";
document.getElementById("boxoverlay").style.display='block';
hidepopup();
}
function hidepopup(){
setTimeout(function(){
document.getElementById("boxoverlay").style.display='none';
},5000)
}
</script>
</html>
我在脚本代码上不应再次显示覆盖层。有任何建议吗?
将 if(popupState!=0){
更改为 if(popupState<0){
。如果在Cookie字符串中找不到name=
,则indexOf
将返回-1
。您可以创建cookie并显示覆盖层。如果cookie确实存在,则可能会放置在其他一堆cookie后面。就我而言,索引为298
。您想检查任何索引0或更高。