Using Javascript to GetCookie
if(getCookie("response") == null)
{
document.getElementById('hide').style.display = 'none';
alert("Yipee");
}
else
{
//on approve creating a new cookie
function a()
{
var a = document.getElementById('approve');
document.getElementById('hide').style.display = 'none';
var expDate = new Date();
expDate.setDate(expDate.getDate() + 7);
document.cookie = 'response=approve;expires=' + expDate.toUTCString();
alert(document.cookie);
}
//on reject creating a new cookie
function r()
{
var a = document.getElementById('reject');
document.getElementById('hide').style.display = 'none';
var expDate = new Date();
expDate.setDate(expDate.getDate() + 7);
document.cookie = 'response=reject;expires=' + expDate.toUTCString();
alert(document.cookie);
}
}
</script>
<body>
<div id="hide">
<form>
<p id="p">Heya!</p>
<input type="button" id='approve' value="approve" onclick="a()"/>
<input type="button" id='reject' value="reject" onclick="r()"/>
</form>
THis is my code...I want to hide an element if a cookie exists and if not, i want to go ahead and display the form. No matter what, the form is a开发者_StackOverflow社区lways displayed and the "yipee" alert box does not appear.
I check with Chrome and it has the cookie called response for localhost.
Thanks for all your help.
maybe instead of getCookie use
c_start=document.cookie.indexOf("mycookie=");
if (c_start!=-1){
//cookie exists
}
If this is a code snippet from the page and it appears in that order, then this couldn't possible work. You test for the existence of the "response" cookie and then attempt to hide DOM elements before the DOM elements are there to hide in the first place.
Try moving the script
block into the bottom of the body
and see if that makes a difference. I think it probably will.
精彩评论