I am building this small cookiescript which creates a cookie when clicking on a button and then hides the message.
But now I am building the function to hide the message (div#cookie) when the cookie has been seet, but I get this error everytime, but my div DOES exist:
Uncaught TypeError: Cannot read property 'style' of null
Now these are the scripts I am using, can anyone help? 🙂
<script type="text/javascript">
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
</script>
and this, where-ever the ‘hidediv’ gives the error, but does work when clicking on a button:
<script type="text/javascript">
function hidediv() {
if (document.getElementById) {
document.getElementById('cookie').style.visibility = 'hidden';
createCookie('uscnCookieScriptJS','uscninternetservicescookiescriptjavascriptversion',365)
}
else {
if (document.layers) {
document.hideShow.visibility = 'hidden';
createCookie('uscnCookieScriptJS','uscninternetservicescookiescriptjavascriptversion',356)
}
else {
document.all.hideShow.style.visibility = 'hidden';
createCookie('uscnCookieScriptJS','uscninternetservicescookiescriptjavascriptversion',356)
}
}
}
function showdiv() {
if (document.getElementById) {
document.getElementById('cookie').style.visibility = 'visible';
}
else {
if (document.layers) {
document.hideShow.visibility = 'visible';
}
else {
document.all.hideShow.style.visibility = 'visible';
}
}
}
</script>
<script type="text/javascript">
if (document.cookie.indexOf("uscnCookieScriptJS") >= 0) {
alert("yes");
hidediv();
}
else {
alert("no");
}
</script>
The script can be found here: http://dev.uscn.nl/cookiescript/v2/
The problem is that you are calling the
hidediv()function before loading the actual HTML content. Either put the script in the end of the document (before closingbodytag) or set it atwindow.onload/ on document ready