For a document loaded into the top frame the parent object is still valid. Intuition might lead you to believe that in this case the parent object would be Null, Nothing, or Empty. (Actually, IsEmpty(parent) always returns true.) This leads to the question: "How can you tell you're in the top frame if you always have a valid parent?" The answer: by checking to see if the parent object is the same as the self object, as the following script (taken from RequestOK.asp) demonstrates:
<script Language=VBScript>
' Only show Close button if this is a stand-alone window
If parent Is self Then
document.write "<br><br><div align=center>"
document.write "<input type=button value=Close " & _
"onClick=""javascript:top.window.close()"">"
document.write "</div>"
End If
</script>
One of the most surprising bugs appeared in the CML application's Add Title Wizard after the upgrade to Internet Explorer 5. When running the wizard with the new browser, the following line of code caused an invalid argument error:
theSel.Add aOpt, i
The object named theSel is a multiselect list box in the parent page that contains names of authors, subjects, or items. The object named aOpt is a new <OPTION> element containing the new author, subject, or item to add to the list box. The script was working perfectly in Internet Explorer 4.01, so what changed? At first we suspected that the integer value was not being translated properly from its VARIANT type (a common, albeit easy bug to catch and fix), but making the conversion explicit didn't make any difference. The first clue came when we realized that similar code was used in AddTitles.asp without an error.
Apparently, the scripting environment in Internet Explorer 5 is quite a bit more sensitive to the scope of objects created by script. When we created the <OPTION> element, we were doing so in the context of the child document object, like this:
Set aOpt = document.createElement("OPTION")
However, in order to add it to the multiselect list box in the parent document, we needed to create it with the parent object, like this:
Set aOpt = parent.document.createElement("OPTION")
Creating the object in the parent scope allowed it to be used by other objects in the same scope.