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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts

    element.id references in HTAs

    Is there an advantage to using the longer syntax, when browsers other than IE are not a consideration, such as:

    document.getElementById("id").attribute = "something";

    vs.

    id.attribute = "something";

    It seems that shorter is better... any reason why not?
    hmm... ?

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Because it's wrong

    It's also less efficient - "globalised" objects are in the document.all collection, which is slower to iterate through, because it's larger and its members are not of a predictable type.
    Last edited by brothercake; 03-12-2004 at 06:43 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's essentially what I'm wondering...
    are you saying that referencing objects with getElementById prevents them from being "globalised", or does that happen regardless?
    hmm... ?

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah IE just does that whatever - every element with a name or ID is put into the global scope, so you have that shortcut referencing.

    It makes it shorter for you to type, but that's the only advantage - it will slow your script down (and get you into bad habits )
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If everything is already global, how would taking advantage of that slow the script down any more than not...?
    hmm... ?

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't understand it myself, but I trust brothercake's reasoning.

    I still don't get it - if all ids are contained in some sort of hashed datastructure (which would make sense, because ids have to be unique), there wouldn't be any need to iterate internally over this datastructure. Hashing the key/id and looking that up would be *much* faster. At least it would opposed to getElementById(), which would need to descend into each element tree, whether it's doing a breadth- or depth-search. Without any prior optimization, in the worst case getElementById would have to check each element before reaching the final correct one. But it's still faster?

    Can we see the code that leads to this conclusion, brothercake? Oh, and if anyone knows how IE does it internally, please speak up or explain why my thoughts above aren't on the mark...
    De gustibus non est disputandum.

  • #7
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Building High Performance HTML Pages

    Scope Your Object References Wisely

    ...

    When Internet Explorer parses a page it accounts for all the objects on the page for which ID attributes have been explicitly specified. It adds them to the global namespace of the scripting engine, allowing you to refer to them directly. That means that the following is excessive.

    <SCRIPT>
    var sText = document.all.div1.innerText;
    </SCRIPT>

    In addition to the extra bytes that are passed across the network and parsed by the scripting engine, four extra calls are made by the script engine back to Internet Explorer in order to retrieve the innerText property of the div. The following is all that's required to retrieve the innerText of the div.

    <SCRIPT>
    var sText = div1.innerText;
    </SCRIPT>

    ...

    When a scripting engine encounters a scoped object model reference in your code, the engine needs to resolve the reference by looking up the left-most piece of that reference in a look-up table. Two factors influence the scripting engine's ability to perform the look-up:

    • The number of entries in the look-up table.
    • The scope of the reference.

    The number of entries in the table corresponds to the number of global variables, named objects, and functions that you have defined on your page. Thus, you should only declare the ID attribute on those elements that you explicitly wish to manipulate through script.

    As mentioned above, there's a right way and a wrong way to scope references. When you've assigned an ID attribute to an element, there's no reason to access the corresponding object through document.all.
    From that, it seems there would be a disadvantage to using document.getElementById (as mordred explained), just as when using document.all.id, in the above description...

    edit: fixed the link...
    Last edited by swmr; 03-12-2004 at 11:24 PM.
    hmm... ?

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Something bothers me about that article:
    The number of entries in the table corresponds to the number of global variables, named objects, and functions that you have defined on your page. Thus, you should only declare the ID attribute on those elements that you explicitly wish to manipulate through script.
    It's entirely unreasonable to expect a developer to optimise their use of ID attributes in order to satisfy this behavior - what if I want ID bindings for CSS? Or just to add semantics to an element? By globalising objects in this way, Internet Explorer is force-reducing the efficiency of my page in general, by adding things to a scope where I may not want them, and where they don't not really belong.

    Now, the point that article makes about shortcut object-referencing is very true - you improve efficiency by creating shortcuts, to save yourself having to traverse the collection every time.

    Fine - you can do that:
    Code:
    var dg = document.getElementById;
    
    var obj = dg["id"];
    But it's arrogant of the browser to assume to do that for you - it doesn't even do that - it creates a set of individual objects by referencing them from the document.all collection. It's equivalent to you doing something like this:
    Code:
    var tags = document.all;
    
    for(var i=0; i<tags.length; i++)
    {
    	if(tags[i].id)
    	{
    		window[tags[i].id] = tags[i];
    	}
    }
    It gives you a visible efficiency with one hand by creating hidden inefficiency with the other. Just like a politician.

    So okay I'll recant my statement that these references are always less efficient - they may not be. And since IE does this whether you want it or not, you may as well take the efficiency improvement and use those references, to make up for the efficiency reduction brought about by their creation.

    But it's still wrong
    Last edited by brothercake; 03-13-2004 at 10:42 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #9
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not wrong! It's a "FEATURE".

    Really though, that's a good point about the evil of auto-global identifiers.
    &nbsp;

    &nbsp;

    Originally posted by mordred
    - (which would make sense, because ids have to be unique) -
    That's another thing which is not necessarily a rule in IE:
    Scripting with Elements and Collections

    Using Collections
    ...
    While in theory ID means "unique identifier," in reality the uniqueness is not enforced. If more than one element has the same identifier, the result is a collection of elements with the same ID, rather than an individual element. The subsequent collection can only be accessed ordinally. If no element has the identifier, the result is null.
    I wonder if using a collection of IDs would actually reduce the size of the look-up table, or if references such as ID[0], ID[1], etc. would be somehow more efficient...
    hmm... ?

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well that's wrong as well - IDs should be unique, not formed into collections. NAME can form collections ... but that's not the same thing.

    I mean really ... MSDN documentation is just ridiculous. You can do all this stuff if you want - but are you so confident it won't get you into bad habits - teach you to do ridiculous and invalid things which may spill over into non-IE scripting?

    Honestly - don't read their technical articles, and don't trust their explanations - they never differentiate between how something should be implemented, and how it's implemented in IE. They don't even acknowledge that there is a difference.
    Last edited by brothercake; 03-13-2004 at 10:39 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeċ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    As for the performance, ie doesn't just place all short forms in the global scope. No, it simply chains the global scope with the document.all vector and other vectors. Which in fact makes a simple call to id less efficient than a call to window.document.all['id'].
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #12
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by brothercake
    are you so confident it won't get you into bad habits - teach you to do ridiculous and invalid things which may spill over into non-IE scripting?
    Well, I don't code by habit (its not a natural act), so there's little danger of proprietary spillage--as I tend to think about each situation independently; my point here is to utilize an optimal code configuration for an HTA--where validity is governed by whatever works best.
    &nbsp;

    &nbsp;

    &nbsp;
    Originally posted by liorean
    ie simply chains the global scope with the document.all vector and other vectors. Which in fact makes a simple call to id less efficient than a call to window.document.all['id'].
    Are you completely contradicting the description provided by the first article? If so, why would the developers misrepresent the behavior of their browser, by stating the opposite?
    hmm... ?

  • #13
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeċ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Originally posted by swmr
    Are you completely contradicting the description provided by the first article? If so, why would the developers misrepresent the behavior of their browser, by stating the opposite?
    Well, if the script is in the global scope already, they are correct. However, if the script is in any other scope, such as an event handler or a function, it will lead to more scope resolving than you gain from it.

    I assume you're not doing any DHTML work in the global scope (since that must mean the document is still loading), and almost all use of this mechanism is DHTML.

    [edit]I like your sig, BTW, swmr!
    Last edited by liorean; 03-14-2004 at 09:46 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #14
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That sounds reasonable... but what sort of additional resolving would be necessary if the element being referred to is already in the global namespace? Isn't it effectively identical to a global variable referenced from within a function?
    hmm... ?

  • #15
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by brothercake
    ...they never differentiate between how something should be implemented, and how it's implemented in IE. They don't even acknowledge that there is a difference.
    There seems to be an acknowledgement here... and it appears to convey their standpoint as "We are the standard!":

    Internet Explorer 6 and Standards
    Microsoft believes very strongly in Internet standards and the standards process, and is committed to implementing appropriate standards when driven by customer demand. However, standards compliance is part of a larger effort that includes many constituencies. By innovating, and driving customer requirements into Internet Explorer and then into the standards groups, we'll make the Internet a richer platform for all users.
    hmm... ?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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