如果存在cookie,则自动执行Javascript

Javascript to execute automatically if cookie exists

本文关键字:执行 Javascript 存在 cookie 如果      更新时间:2023-12-04

我正在尝试制作一个cookie策略通知弹出窗口,它按照我想要的方式工作,并创建一个cookie。然而,由于我是javascript(加上任何类型的编码)的新手,我不知道如何使用另一个脚本来搜索cookie"STLCSD",如果存在,则运行"hideDiv"函数。

我想避免使用HTML、CSS和JS之外的任何东西,因为这个机构使用的平台不支持其他任何东西。

任何帮助都将不胜感激。只需询问任何进一步的细节

<script language=javascript type='text/javascript'> 
function hideDiv() { 
if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById('hideShow').style.height= '0px'; 
document.getElementById('hideShow').style.fontSize= '0px'; 
document.getElementById('hideShow').style.color= '#cddfea';
document.getElementById('hideShow').style.top= '-10px';
document.getElementById('hideShow').style.backgroundColor= '#cddfea';
document.getElementById('topbar').style.height= '1px';

} 
else { 
if (document.layers) { // Netscape 4 
document.hideShow.height= '0px'; 
document.hideShow.fontSize= '0px';
document.getElementById('hideShow').style.color= '#cddfea';
document.getElementById('hideShow').style.top= '-10px';
document.getElementById('hideShow').style.backgroundColor= '#cddfea';
document.getElementById('topbar').style.height= '1px';

} 
else { // IE 4 
document.all.hideShow.style.height= '0px'; 
document.all.hideShow.style.fontSize= '0px'; 
document.getElementById('hideShow').style.top= '-10px';
document.getElementById('hideShow').style.color= '#cddfea';
document.getElementById('hideShow').style.backgroundColor= '#cddfea';
document.getElementById('topbar').style.height= '1px';

} 
} 
} 
function cookiepolicy(){
   days=50; // number of days to keep the cookie
   myDate = new Date();
   myDate.setTime(myDate.getTime()+(days*24*60*60*1000));
   document.cookie = 'cookieName=STLCSD; expires=' + myDate.toGMTString();
}

</script> 
<style>
#hideShow{
color:white;
font-family:Gill Sans MT;
text-align:center;
overflow:hidden;
font-size:20px;
height:35px;
width:100%;
transition:.3s;
background-color:#0e589e;
margin:0px;
padding:3px;
top:0px;
position:absolute;
}
#hideshow a:link{
color:#0e589e;
font-style:bold;
background-color:#cddfea;
padding-left:9px;
padding-right:9px;
padding-top:0px;
padding-bottom:0px;
text-decoration:none;
margin:0px;
right:40px;
position:absolute;
}
#topbar{
color:white;
font-family:Gill Sans MT;
text-align:center;
overflow:hidden;
font-size:20px;
height:35px;
width:100%;
transition:.3s;
background-color:#cddfea;
margin:0px;
padding:0px;
top:0px;
}

</style>
<div id="topbar">
</div>
<div id="hideShow" ..etc> 
<b>Cookie Policy</b>
This site uses cookies to store information on your computer.
<a href="javascript:hideDiv()" onclick="cookiepolicy()">Accept</a> 
</div> 

我使用了这样一个函数:

function readCookie(cookieName)
{
  if(document.cookie.length > 0)
  {
    var begin = document.cookie.indexOf(cookieName + "=");
    if(begin != -1) {
        begin = begin + cookieName.length + 1;
        var end = document.cookie.indexOf(";",begin);
        if (end == -1) end = document.cookie.length;
        return unescape(document.cookie.substring(begin,end));
    } else {
       return "";
    }
  }
  return "";
}

之后,你可以使用类似的东西:

if(readCookie("STLCSD") == "true") { hideDiv(); }

我希望它能有所帮助!