View Full Version : My getElementById('pc') and getElementsByTagName('BODY') give errors.

Jules Manson
01-23-2006, 09:43 AM
For the most part I do know how to use these two functions as I have used them before. The problem is that when I point to the body tag it creates errors. I am using Windows XP Home edition and this bit of script produces errors on MSIE 6.0 and the script is completely ignored on Firefox 1.5 and Opera 8.51. I prefer to point to the body tag because it is the easiest and most convenient place to navigate through the DOM tree especially since the elements that I intend to create will become the first child of my body tag.

The error I get when using getElementById on MSIE is "Error: docBody is null or not an object." The error I get when using getElementsByTagName on MSIE is "Error: Object doesn't support this property or method." :confused:

The ultimate goal of my script is to create the following HTML as a first child of the body tag. However for now I will happily settle on why I am getting errors.

<DIV class="topanchor"><A name="top" class="ghost">top</A></DIV>

The complete HTML is:

<TITLE>Request For Help - PageBuilder</TITLE>
// var docBody = document.getElementById('pc');
var docBody = document.getElementsByTagName('body')[0];
firstDiv = docBody.appendChild(document.createElement('DIV'));
firstDiv.className = 'topanchor';
topAnchor = firstDiv.appendChild(document.createElement('A'));
topAnchor.className = 'ghost';
topAnchor.name = 'top';
<BODY ID="pc">

I really do appreciate help offered as this is evident in the countless hours that I have spent in instant messaging helping others with Windows anamolies which is something that I am very knowledgable in. Please test your solution before providing one. If you cannot provide an adequate answer to my query please do not respond to this posting. Useless responses tend to make postings long, difficult to follow, and old. Thank you for your cooperation.

Jules Manson
01-23-2006, 09:55 AM
Nevermind good people with the help of Kor at webdeveloper.com I found a solution. :)

01-23-2006, 01:38 PM
for anyone interested, Kor's solution can be seen here...