Javascript place in header

Posted by JibW on Stack Overflow See other posts from Stack Overflow or by JibW
Published on 2011-11-27T17:47:21Z Indexed on 2011/11/27 17:49 UTC
Read the original article Hit count: 215

Filed under:
|

I am not clear why its not working the javascript code when I add it to the header section as follows.

We can place a javascript code within the body as follows

<html> 

 <head>
    <title> Simple Test </title>    
 </head>
 <body>
        <div id="mydiv"> This is the div content </div>

        <script type="text/javascript" >
           document.getElementById("mydiv").innerHTML=Date();         
        </script>   

 </body>
</html>

But when I place the same JavaScript code in the header section it doesn't work.

<html>
  <head>
   <title> Simple Test </title>

   <script type="text/javascript" >
      document.getElementById("mydiv").innerHTML=Date();         
   </script>    

  </head>

Can Someone please explain the issue. I know I can Write a JavaScript function in header and call it in an event. But can't we Use in this way. If Can't why.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html