Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: DOM Object

  1. #1
    New to the CF scene
    Join Date
    Jun 2002
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    DOM Object

    What is document.all used for?

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Helsinki, Finland
    Posts
    231
    Thanks
    0
    Thanked 1 Time in 1 Post
    Long time ago before The Great Rain of Rocks™, there were many opinions (point of views), how html-document should be handled as an object. (thinking: jkd probably explains this better due to my poor skill of English, but I still try)

    IE4 : document.all[objectID]
    NN4 : document.layers(layerName)
    DOM : document.getElementById(objectId)

    Follow the conversation here in forums, and you'll learn basics of DOM in no time. That's the best way to get yourself familiar with document object modelling. Also, visit W3C Homepage for exact DOM Core Recommendations.
    Zvona
    First Aid for
    Web Design

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    document.all was an interesting, proprietary object introduced by MS in IE4 to compete with NS4's Layer model.

    It is really an array, but since MS liked VBScripter's, they also allowed the use of () indexing, like in VB.

    Which means you had several ways to use it:

    document.all['someIDorName']
    is the same as:
    document.all('someIDorName')

    Since more than one element is allowed the same name attribute, sometimes you get a collection returned to you:

    document.all('someName').length > 1

    Which meant you either went like:

    document.all('someName')[X]
    OR, since VBScript has a special syntax for nested arrays:
    document.all('someName', X);

    Where X is the offset of the node in the collection.

    If that isn't enough, you can specify a numerical index in the all array:

    document.all[0] // document.all(0)

    returned the very first node in the document for example.

    On top of all that, you could also get collections of all nodes with the same tag:

    document.all.tags('tagname')

    Then you access the offset with array notation. You can also access elements the proper way from collections with item(X), though I'm not sure if IE4 had that method.


    All those abilities are covered in the W3C DOM1 Core, so there is no need to use that notation anymore:

    Get an element by its id:
    document.getElementById('id');


    Get elements by their name:
    document.getElementsByName('name')
    That returns a collection, so use item(X) or [X] notation to get it

    Get a collection of tags:
    Node.prototype.getElementsByTagName('tagname')

    I say Node.prototype because all nodes have this method, and return only childnodes of that node.
    The most typical use is
    document.getElementsByTagName('bla');
    This method returns a collection once again.

    That aid any?

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Also, I think I'm going to move this to the DOM Scripting forum, even though it is about document.all, one could argue that is an older version of a DOM.

  • #5
    New Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So, document.all is used by IE4, document.layer by NS4, and newer browsers (that are W3C-compliant) use document.getElementById (or getElementsByName or getElementsByTagName). That is correct, right?

    So, if I want to change something about a DIV with the id, MyDiv for example, such as perhaps its innerHTML or its display style, I need to create a variable, divToChange for example, with the path to the div depending on which browser is used. How would I test for which method to use and then use it. I assume "if () { } if () { } else {}" is needed but what goes in the normal brackets?
    skinme.net / v2.php (design nearly completed)

  • #6
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    var div;
    if (typeof document.layers != 'undefined') div = document.layers['MyDiv'];
    else if (typeof document.all != 'undefined') div = document.all['MyDiv'];
    else if (typeof document.getElementById != 'undefined') div = document.getElementById('MyDiv');


    That is really the long way around it, and is a prime example of why people need to upgrade to standards compliant browers.

    You can shortcut around NS4 though if you don't want to support it:

    var div = (document.getElementById || document.all)('MyDiv');


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •