Client-side DOM: are 'window' and 'document' real objects?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hermit Crab

    #1

    Client-side DOM: are 'window' and 'document' real objects?

    I'm seeing some unexpected (at least to me) behavior in the 'window' and
    'document' objects under Netscape 7.1 and Internet Explorer 6.0.

    When a property is added to the prototype for 'Object', it was my
    understanding that it should be visible to all objects.

    But it appears that it is NOT visible in the 'window' object of either IE or
    Netscape.
    It is visible in the 'document' property of Netscape, but not IE.
    It is visible in user-created objects and object literals in both Netscape
    and IE (as expected).

    Thus it appears that 'window' and 'document' do not act like normal objects.

    Also, under IE 'window' and 'document' do not have constructor properties,
    whereas in Netscape they are defined as 'Window' and 'HTMLDocument'
    respectively. This seems particularly strange since even object literals
    have a constructor property (in both browsers).

    1) Is this the correct behavior for the 'window' object?
    2) Is the Netscape or IE behavior correct for the 'document' object?
    3) Can anyone point me to appropriate documentation for the behavior or
    these objects?

    Below is an HTML page which demonstrates this behavior.

    If anyone could shed any light on what is going on here (or point me to a
    good resource), I would be very grateful.

    Thank you.


    <html>
    <head>
    <title>window object test</title>
    <script type="text/javascript">
    function showInfo() {
    var dateObject = new Date();
    var literalObject = {x:1, y:2};
    alert("window.c onstructor = " + window.construc tor);
    // 'undefined' in IE, 'Window' in Netscape
    alert("document .constructor = " + document.constr uctor);
    // 'undefined' in IE, 'HTMLDocument' in Netscape
    alert("dateObje ct.constructor = " + dateObject.cons tructor);
    // 'Date' in both browsers
    alert("literalO bject.construct or = " + literalObject.c onstructor);
    // 'Object' in both browsers
    Object.prototyp e.testProperty = "Hello";
    alert("window.t estProperty = " + window.testProp erty);
    // 'undefined' in IE and Netscape
    alert("document .testProperty = " + document.testPr operty);
    // 'undefined' in IE, "Hello" in Netscape
    alert("dateObje ct.testProperty = " + dateObject.test Property);
    // 'Hello' in both browsers
    alert("literalO bject.testPrope rty = " + literalObject.t estProperty);
    // 'Hello' in both browsers
    }
    </script>
    </head>
    <body onload="showInf o();">
    <h3 align="center"> window object test</h3>
    </body>
    </html>


  • Richard Cornford

    #2
    Re: Client-side DOM: are 'window' and 'document' real objects?

    "Hermit Crab" <hermitcrab@att .net> wrote in message
    news:vXNDb.4701 08$0v4.21258518 @bgtnsc04-news.ops.worldn et.att.net...
    <snip>[color=blue]
    >Thus it appears that 'window' and 'document'
    >do not act like normal objects.[/color]

    The ECMA 262 3rd Edition specification would categorise document (and
    all of the DOM elements/nodes under it) as a "Host Objects" (section
    4.3.8) and makes no requirements of host objects. On web browsers the
    window object is also the object that ECMA 262 refers to as the global
    object (Section 15.1) and the global object is specified as having no
    [[Constructor]] property and implementation dependent [[Prototype]] and
    [[Class]] properties.

    <snip>[color=blue]
    >1) Is this the correct behavior for the 'window' object?[/color]

    Yes, in so far as it is not incorect.
    [color=blue]
    >2) Is the Netscape or IE behavior correct for the
    > 'document' object?[/color]

    The only criteria of correctness available for the document object are
    the W3C Core and HTML DOM specifications and Netscape 6+ and IE 5.5+
    more or less conform to that specification in their implementations .
    But the W3C DOM specifications define interfaces and say nothing about
    prototypes, constructors or inheritance (because they are intended to be
    language neutral).
    [color=blue]
    >3) Can anyone point me to appropriate documentation for
    > the behavior or these objects?[/color]
    <snip>

    ECMA 262 and the W3C DOM specifications are referenced in the
    comp.lang.javas cript FAQ:-

    <URL: http://jibbering.com/faq/ >

    Richard.


    Comment

    • Hermit Crab

      #3
      Re: Client-side DOM: are 'window' and 'document' real objects?

      "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
      news:broda4$jal $1$8302bc10@new s.demon.co.uk.. .[color=blue]
      > The ECMA 262 3rd Edition specification would categorise document (and
      > all of the DOM elements/nodes under it) as a "Host Objects" (section
      > 4.3.8) and makes no requirements of host objects.[/color]

      <snip of extremely helpful reply>

      Thanks very much for you reply. I took a look at the sections of ECMA-262
      that and would like to verify that I'm understanding it correctly. It seems
      that:

      1) Host objects may, but are not required to inherit functionality from
      Object.

      2) Attempting at add functionality to any 'host' object via the prototype
      property of the constructor should be avoided. Depending upon the
      implementation, the updates to the prototype property may not be allowed.
      Adding properties to a prototype should only be done for user-defined
      objects and those specified in ECMA-262.

      Have I interpreted you response correctly?

      In any event, you've pointed me in the right direction. I hadn't really
      been aware of the differences between 'host' and 'native' objects. I'll go
      back and do some more reading.

      Thanks for taking the time to help me.
      ..



      Comment

      • Richard Cornford

        #4
        Re: Client-side DOM: are 'window' and 'document' real objects?

        "Hermit Crab" <hermitcrab@att .net> wrote in message
        news:4CPDb.4706 35$0v4.21271096 @bgtnsc04-news.ops.worldn et.att.net...
        <snip>[color=blue]
        >... . It seems that:
        >
        >1) Host objects may, but are not required to inherit
        >functionalit y from Object.[/color]

        Yes.
        [color=blue]
        >2) Attempting at add functionality to any 'host' object
        >via the prototype property of the constructor should be
        >avoided.[/color]

        That might be a bit strong. Generally I would tend to avoid modifying
        the prototype of host objects (if available) but if handled with the
        caution that is required in any reliable client-side script it could be
        a more efficient alternative to a less direct method.

        Though I don't see much point in modifying the document or the window
        via their prototypes as there is only one of each and they can be
        modified directly with the same effort.
        [color=blue]
        >Depending upon the implementation, the updates to the
        >prototype property may not be allowed.[/color]

        Yes, and there may be no publicly accessible prototype property or
        constructor.
        [color=blue]
        >Adding properties to a prototype should only be done for
        >user-defined objects and those specified in ECMA-262.[/color]

        User-defined objects are certainly where this type of functionality can
        be expected to work reliably.
        [color=blue]
        >Have I interpreted you response correctly?[/color]
        <snip>

        Yes.

        Richard.


        Comment

        Working...