Issue with document.getElementById accessing HEAD id = "head"

I turn to the chapter

var head = document.getElementById ("head");

Works with IE, not Firefox ..

+2


source to share


4 answers


Use this instead:



document.getElementsByTagName("head")[0]

      

+3


source


I think the attribute id is not allowed in this particular tag.



+2


source


<html>
 <head id="head">
  <script type="text/javascript" src="jquery-1.3.1.min.js" ></script>
 </head>
 <body>

 <script type="text/javascript">
  alert(document.getElementById("head"));
 </script>
 </body>
</html>

      

works great in firefox. but if you script is in header and runs automatically, it cannot work as header is not fully loaded

+1


source


Tested on Firefox 3 with Firebug:

>>> document.getElementById('head')
<head id="head">

      

Are you sure your head element has a head ID?

0


source







All Articles