如果cookie存在则自动执行Javascript

时间:2013-11-29 10:31:12

标签: javascript function cookies automation policy

我正在尝试制作一个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> 

1 个答案:

答案 0 :(得分:2)

我使用了这样的函数:

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

我希望它有所帮助!