views:

68

answers:

3

I have the following code, and the javascript console in chrome says "Can't read innerHTML property of null. Why does document.getElementById('display') turn up empty handed?

<!DOCTYPE html>
<html>
    <head>
        <title>Chat 3</title>
        <script type="text/javascript">
            function showmsg(str){
                var display = document.getElementById('display');
                display.innerHTML += "<p>" + str + "</p>";
            }

            if("WebSockets" in window){
                pass;
            }else{
                showmsg("Your browser doesn't support WebSockets. Try Google Chrome.");
            }
        </script>

        <style type="text/css">
        #username { 
            padding: 0px;
            margin: 0px;
            height: 26px;
            width: 400px;
        }

        /* ADDED container div that wraps onlineusers and display */
        #container {
          margin: 10px 0;
        }

        /* use float: left to put them side-by-side */
        #display { 
            padding: 0px;
            margin: 0px;
            border-style: solid;
            border-width: 1px;
            overflow: auto;
            height: 400px;
            width: 400px;
            float: left;
        }

        #onlineusers { 
            padding: 0px;
            margin: 0px;
            height: 400px;
            width: 200px;
            border-style: solid;
            border-width: 1px;
            float: left;
        }

        /* Added container2 to wrap inputline and sendbutton */
        #container2 {
            margin: 10px 0;
        }

        #inputline { 
            padding: 0px;
            margin: 0px;
            height: 26px;
            width: 350px;
            float: left;
        }

        #sendbutton {
            padding: 0px;
            margin: 0px;
            height: 30px;
            width: 50px;
            float: left;
        }

        /* this is a well used "hack". */
        .clearfix {
          clear: both;
        }
        </style>
    </head>
    <body onload="document.getElementById("username").focus()">
            <input type="text" id="username" />
            <div id="container">
                <div id="display"></div>
                <div id="onlineusers"></div>
                <div class="clearfix"></div>
            </div>

            <div id="container2">
                <input type="text" id="inputline" length="55" />
                <input type="button" id="sendbutton" value="Send" />
            </div>
    </body>
</html>
+5  A: 

You're calling showmsg before the whole page (and hence the display div) is loaded. Hence the error. Call it from onload and it'll work.

Add this function to the <head>

function handleLoad()
{
  document.getElementById("username").focus()
  if("WebSockets" in window){
   pass;
  }else{
    showmsg("Your browser doesn't support WebSockets. Try Google Chrome.");
  }
}

and call it from onload

<body onload="handleLoad()">
Amarghosh
+1  A: 

Put your script tag at the end of the document:. In your case the page has not been completely parsed into DOM tree, but your script executes anyway:

       <body onload="document.getElementById('username').focus()">
           <input type="text" id="username" />
           <div id="container">
               <div id="display"></div>
               <div id="onlineusers"></div>
               <div class="clearfix"></div>
           </div>

           <div id="container2">
               <input type="text" id="inputline" length="55" />
               <input type="button" id="sendbutton" value="Send" />
           </div>

           <script type="text/javascript">
           function showmsg(str){
               var display = document.getElementById('display');
               display.innerHTML += "<p>" + str + "</p>";
           }

           if("WebSockets" in window){
              pass;
           }else{
               showmsg("Your browser doesn't support WebSockets. Try Google Chrome.");
           }
       </script>
   </body>

This is also considered a good practice for performance reasons: http://developer.yahoo.com/performance/rules.html#js_bottom

naikus
+4  A: 

Move your script-section down to the bottom of the page. Otherwise it is executed before the page has been loaded.

Btw: Putting scripts at the bottom of your page is even a "best practice" and recommended by Google, Yahoo & Co

Gerhard Dinhof
+1 for suggesting the yahoo link. I did not see it before (when i edited my answer)
naikus