...

View Full Version : element.id references in HTAs



swmr
03-12-2004, 06:40 PM
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?

brothercake
03-12-2004, 07:40 PM
Because it's wrong :p

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.

swmr
03-12-2004, 07:53 PM
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?

brothercake
03-12-2004, 08:22 PM
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 ;))

swmr
03-12-2004, 08:31 PM
If everything is already global, how would taking advantage of that slow the script down any more than not...? :confused:

mordred
03-12-2004, 09:15 PM
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...

swmr
03-13-2004, 12:19 AM
Building High Performance HTML Pages (http://msdn.microsoft.com/workshop/author/perf/perftips.asp)

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...

brothercake
03-13-2004, 06:19 PM
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:


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:


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 :o 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 :p

swmr
03-13-2004, 10:41 PM
Not wrong! It's a "FEATURE". :D

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 (http://msdn.microsoft.com/workshop/author/om/scripting_elements_collections.asp)

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...

brothercake
03-13-2004, 11:33 PM
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.

liorean
03-13-2004, 11:41 PM
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'].

swmr
03-14-2004, 01:56 AM
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? :confused:

liorean
03-14-2004, 03:06 AM
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? :confused:
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!

swmr
03-14-2004, 03:59 AM
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?

swmr
03-14-2004, 08:03 AM
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!": :D


Internet Explorer 6 and Standards (http://msdn.microsoft.com/library/en-us/dndude/html/dude03262001.asp)
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.

Code Wizard
03-14-2004, 01:11 PM
Originally posted by swmr
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?

Please explain, what do you mean by objects being globalised ?

swmr
03-14-2004, 06:54 PM
The knowledgeable contributers (not me) to the following thread have explained the concept:
Local VS Global (http://codingforums.com/showthread.php?&threadid=24815)

There's also this:
Variable Scope (http://msdn.microsoft.com/library/en-us/script56/html/js56jsconscope.asp)

and the first article I referred to in this thread...

Code Wizard
03-14-2004, 08:21 PM
Thank you,you are so kind :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum