2

I have the following script in my head section:

 1| <script type="text/javascript">
 2| var inAccount = 0;
 3| if (inAccount = 0){
 4|   document.getElementById("L-out").style.display = 'block';
 5|   document.getElementById("User").style.display = 'block';
 6| }else{
 7|   document.getElementById("User").style.display = 'none';
 8|   document.getElementById("L-out").style.display = 'none';
 9| }
10| </script>

And the following code in my body section:

<div id="log">
<a href="#" class="in" id="Sign-up">Sign up</a>
<a href="#" class="in" id="L-in">Login</a>
<a href="#" class="in" id="User">Account</a>
<a href="#" class="in" id="L-out">Logout</a>
</div>

When I load the page I get this error message: 'null' is not an object. Referring to line 7.

Does any one know what might be happening?

Nobel Chicken
  • 1,235
  • 10
  • 13

5 Answers5

3

The DOM elements referred in your script don't exist yet when the script is run (that's why the reference is null). To solve, just move that script on a window.load/domready event handler (or simply put that script as last child of the body element)

As a sidenote, the equality check should be made with ===

Fabrizio Calderan loves trees
  • 109,094
  • 24
  • 154
  • 160
0

Dom is not ready.Wrap your code

window.onload = function(){ 
 if (inAccount == 0){
     document.getElementById("L-out").style.display = 'block';
     document.getElementById("User").style.display = 'block';
  }else{
     document.getElementById("User").style.display = 'none';
    document.getElementById("L-out").style.display = 'none';
   }
});
Suresh Atta
  • 114,879
  • 36
  • 179
  • 284
0

Make sure <script> tags are at the bottom of your document [just above </body>] (for a couple of reasons):

  1. Makes the page being rendered first priority (site appears faster to end-user).
  2. The DOM objects will be created at that point, so JS will be able to locate the elements (like you're most likely having trouble with).

You can bind to document ready at the top, but I still am a firm believer of HTML first, JS second.

Also, when doing a compare in JavaScript, use == (or for a type-specific compare ===). You're using an assignment in your first if(...) statement.

Brad Christie
  • 96,086
  • 15
  • 143
  • 191
0

What's happening is your code is looking for elements that haven't actually loaded yet! What you can do to fix this is wrap your current if-else statement into a new function (function init() {...} for example), then in your body tag:

<body onload="init();">

Also, take care to use == or === in your if-statement.

Chris Forrence
  • 9,648
  • 11
  • 43
  • 59
0
  1. use == when comparing variables in javascript
  2. import jQuery in your page to make your js cross browser

    $(document).ready(function(){
        var inAccount = 0;
        if (inAccount == 0){
            $("#User").show();
            $("#L-out").show();
        }else{
            $("#Sign-up").show();
            $("#L-in").show();
        }
    });
    

here's fiddle: http://jsfiddle.net/5wER6/

Nitin S
  • 5,628
  • 9
  • 46
  • 85