1

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/

Chloë Vd Eeden
  • 31
  • 1
  • 1
  • 7
  • You have no element hideShow in the HTML, but this should lead the error only in browsers that do not support getElementById – Maxim Krizhanovsky Aug 03 '12 at 09:20
  • 1
    browsers that don't support getElementById (srsly?) would just throw an error that getElementById is not a function or similar. You wouldn't see style is null or whatever. – Esailija Aug 03 '12 at 09:23
  • maybe, The problem is that you are calling the javascript function before loading the actual HTML content.. – T.Todua Sep 25 '13 at 20:51

1 Answers1

4

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 closing body tag) or set it at window.onload / on document ready

Maxim Krizhanovsky
  • 24,757
  • 5
  • 49
  • 85