tarh331_hax0r
03-06-2005, 08:07 PM
This is a question about the Document Object Model [the DOM] and Internet Explorer's "document" object.
According to this page at MSDN, a "document" object always has a member object named "body":
http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_document.asp
Now if I declare "<BODY>" explicitly, then the following code works just fine, and Internet Explorer reports that the document does indeed have a non-null body:
<body>
</body>
<script language="javascript">
if(document.body == null)
{
window.alert("\"document.body\" is null.");
}
else
{
window.alert("\"document.body\" is NOT null.");
}
</script>
However, if I do NOT declare "<BODY>" explicitly, then Internet Explorer reports that the document has a null body:
<script language="javascript">
if(document.body == null)
{
window.alert("\"document.body\" is null.");
}
else
{
window.alert("\"document.body\" is NOT null.");
}
</script>
MY QUESTION: If you do not declare "<BODY>" explicitly, is there some way to get the null pointer that lives at "document.body" to point to something that isn't null? Or are you stuck with a null body for the lifetime of the document?
Note that while "document" does have a createElement method, there is almost nothing else to work with: No appendChild, no swapNode, no replaceNode, and no removeNode.
For instance, the following won't work, because "document" doesn't have an appendChild method:
<script language="javascript">
var theBody = document.createElement("BODY");
document.body = document.appendChild(theBody);
</script>
I tried about a gazillion other ways to do this, but nothing seemed to work.
On a related note, if "<BODY>" is not declared explicitly, note that Internet Explorer will instantiate a "<HEAD>" object and place the javascript code therein. This is particularly strange, because there is no "document.head" object in the DOM hierarchy. You also see this with "document.title": If you set "document.title" to be something non-null, then Internet Explorer places the "<TITLE>" code within the "<HEAD>" code, so that it seems like this really ought to be an object known as "document.head.title" rather than just "document.title" [but of course, there is no "document.head" to contain a member object named "title"].
Now if we ask a document with no "<BODY>" to return all of its "<HEAD>" objects and then walk the tree from there, we find that the parentNode is non-null and has innerHTML, that the "grandParentNode" is non-null but lacks innerHTML, and finally that the "greatGrandParentNode" is null. My naive guess would be that the parentNode is something like "window.document" and that the grandparent is something like just plain old "window", but "window.document" is not supposed to have an innerHTML attribute [at least not according to the documentation sited above]:
<script language="javascript">
var theAlertString = "";
var theHeaderList = document.getElementsByTagName("HEAD");
theAlertString = "BREAKPOINT 1: ";
if(theHeaderList.length == 0)
{
theAlertString += "The document has no <HEAD>.";
}
else
{
theAlertString += "The document has a <HEAD>, and theHeaderList[0].innerHTML is as follows.\n";
theAlertString += theHeaderList[0].innerHTML;
}
window.alert(theAlertString);
theAlertString = "BREAKPOINT 2: ";
if(theHeaderList[0].parentNode == null)
{
theAlertString += "theHeaderList[0].parentNode is null.";
}
else
{
theAlertString += "theHeaderList[0].parentNode is NOT null, and has the following innerHTML.\n";
theAlertString += "\n";
theAlertString += theHeaderList[0].parentNode.innerHTML;
}
window.alert(theAlertString);
theAlertString = "BREAKPOINT 3: ";
if(theHeaderList[0].parentNode.parentNode == null)
{
theAlertString += "theHeaderList[0].parentNode.parentNode is null.";
}
else
{
theAlertString += "theHeaderList[0].parentNode.parentNode is NOT null, and has the following innerHTML.\n";
theAlertString += "\n";
theAlertString += theHeaderList[0].parentNode.parentNode.innerHTML;
}
window.alert(theAlertString);
theAlertString = "BREAKPOINT 4: ";
if(theHeaderList[0].parentNode.parentNode.parentNode == null)
{
theAlertString += "theHeaderList[0].parentNode.parentNode.parentNode is null.";
}
else
{
theAlertString += "theHeaderList[0].parentNode.parentNode.parentNode is NOT null, and has the following innerHTML.\n";
theAlertString += "\n";
theAlertString += theHeaderList[0].parentNode.parentNode.parentNode.innerHTML;
}
window.alert(theAlertString);
</script>
Any help would be most appreciated! Thanks!
According to this page at MSDN, a "document" object always has a member object named "body":
http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_document.asp
Now if I declare "<BODY>" explicitly, then the following code works just fine, and Internet Explorer reports that the document does indeed have a non-null body:
<body>
</body>
<script language="javascript">
if(document.body == null)
{
window.alert("\"document.body\" is null.");
}
else
{
window.alert("\"document.body\" is NOT null.");
}
</script>
However, if I do NOT declare "<BODY>" explicitly, then Internet Explorer reports that the document has a null body:
<script language="javascript">
if(document.body == null)
{
window.alert("\"document.body\" is null.");
}
else
{
window.alert("\"document.body\" is NOT null.");
}
</script>
MY QUESTION: If you do not declare "<BODY>" explicitly, is there some way to get the null pointer that lives at "document.body" to point to something that isn't null? Or are you stuck with a null body for the lifetime of the document?
Note that while "document" does have a createElement method, there is almost nothing else to work with: No appendChild, no swapNode, no replaceNode, and no removeNode.
For instance, the following won't work, because "document" doesn't have an appendChild method:
<script language="javascript">
var theBody = document.createElement("BODY");
document.body = document.appendChild(theBody);
</script>
I tried about a gazillion other ways to do this, but nothing seemed to work.
On a related note, if "<BODY>" is not declared explicitly, note that Internet Explorer will instantiate a "<HEAD>" object and place the javascript code therein. This is particularly strange, because there is no "document.head" object in the DOM hierarchy. You also see this with "document.title": If you set "document.title" to be something non-null, then Internet Explorer places the "<TITLE>" code within the "<HEAD>" code, so that it seems like this really ought to be an object known as "document.head.title" rather than just "document.title" [but of course, there is no "document.head" to contain a member object named "title"].
Now if we ask a document with no "<BODY>" to return all of its "<HEAD>" objects and then walk the tree from there, we find that the parentNode is non-null and has innerHTML, that the "grandParentNode" is non-null but lacks innerHTML, and finally that the "greatGrandParentNode" is null. My naive guess would be that the parentNode is something like "window.document" and that the grandparent is something like just plain old "window", but "window.document" is not supposed to have an innerHTML attribute [at least not according to the documentation sited above]:
<script language="javascript">
var theAlertString = "";
var theHeaderList = document.getElementsByTagName("HEAD");
theAlertString = "BREAKPOINT 1: ";
if(theHeaderList.length == 0)
{
theAlertString += "The document has no <HEAD>.";
}
else
{
theAlertString += "The document has a <HEAD>, and theHeaderList[0].innerHTML is as follows.\n";
theAlertString += theHeaderList[0].innerHTML;
}
window.alert(theAlertString);
theAlertString = "BREAKPOINT 2: ";
if(theHeaderList[0].parentNode == null)
{
theAlertString += "theHeaderList[0].parentNode is null.";
}
else
{
theAlertString += "theHeaderList[0].parentNode is NOT null, and has the following innerHTML.\n";
theAlertString += "\n";
theAlertString += theHeaderList[0].parentNode.innerHTML;
}
window.alert(theAlertString);
theAlertString = "BREAKPOINT 3: ";
if(theHeaderList[0].parentNode.parentNode == null)
{
theAlertString += "theHeaderList[0].parentNode.parentNode is null.";
}
else
{
theAlertString += "theHeaderList[0].parentNode.parentNode is NOT null, and has the following innerHTML.\n";
theAlertString += "\n";
theAlertString += theHeaderList[0].parentNode.parentNode.innerHTML;
}
window.alert(theAlertString);
theAlertString = "BREAKPOINT 4: ";
if(theHeaderList[0].parentNode.parentNode.parentNode == null)
{
theAlertString += "theHeaderList[0].parentNode.parentNode.parentNode is null.";
}
else
{
theAlertString += "theHeaderList[0].parentNode.parentNode.parentNode is NOT null, and has the following innerHTML.\n";
theAlertString += "\n";
theAlertString += theHeaderList[0].parentNode.parentNode.parentNode.innerHTML;
}
window.alert(theAlertString);
</script>
Any help would be most appreciated! Thanks!