Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

<FAQENTRY> 4.41 correction

0 views
Skip to first unread message

Křištof Želechovski

unread,
Mar 3, 2007, 2:18:10 PM3/3/07
to
At <http://jibbering.com/faq/index.html#FAQ4_41>
IS
Microsoft introduced a shortcut that can be used to reference elements which include an ID attribute where the ID becomes a global variable.
SHOULD BE
Microsoft introduced a shortcut that can be used to reference elements THAT ARE NOT FORM CONTROLS WITHIN A FORM ELEMENT which include an ID attribute where the ID becomes A PROPERTY OF THE GLOBAL WINDOW OBJECT and the scope of the window object is at the bottom of the symbol lookup stack..
In other words, if your design for Microsoft Internet Explorer, you should use the syntax WINDOW.DEVID to avoid name clash and make symbol lookup more efficient.
Chris

VK

unread,
Mar 3, 2007, 3:19:46 PM3/3/07
to
On Mar 3, 10:18 pm, Křištof Želechovski <giecr...@stegny.2a.pl> wrote:
> At <http://jibbering.com/faq/index.html#FAQ4_41>

> SHOULD BE
> Microsoft introduced a shortcut that can be used to reference
> elements THAT ARE NOT FORM CONTROLS WITHIN A FORM ELEMENT
> which include an ID attribute

That is not true for Firefox 1.5 in quirk mode (no appropriate DOCTYPE
switcher). IDed form controls are affected as well by this extension.
A regular payback for trying to monkey out undocumented 3rd party
quirks - high risk to get it either wrong or "too right".

IMHO i) either we leave it as it is as it is better to overcover than
undercover or ii) we are making a quirks table for all browsers up-to-
date emulated the behavior in question.

Which one is the best I don't know. What do you think?

> where the ID becomes A PROPERTY OF THE GLOBAL WINDOW OBJECT and the
> scope of the window object is at the bottom of the symbol lookup
> stack..

There is not such thing as "global window object". There is Global
object representing the current script execution context and there is
window host object which is normally presented and - if presented -
placed before the Global. This way by crafting window properties one
can "shadow" global variables in Global scope, but it is not the best
way. It is better to pre-declare - if possible - all global variables
used in the script by using var statement. This kill the unwanted id -
var echoing.

P.S. For window host object and Global object differences read MSDN
and older topics in this group. For practical demonstration on IE it
requires to create parallel Global scope using behavior. Firefox is
much easier on this matter, something like:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script>

function init() {
alert(typeof txt);
window.txt = true;
alert(typeof txt);
delete(window.txt);
alert(typeof txt);
}

window.onload = init;
</script>
</head>
<body>
<form method="post" action="">
<input type="text" name="txt" id="txt">
</form>
</body>
</html>


VK

unread,
Mar 3, 2007, 3:43:29 PM3/3/07
to
On Mar 3, 11:19 pm, "VK" <schools_r...@yahoo.com> wrote:
> It is better to pre-declare - if possible - all global variables
> used in the script by using var statement. This kill the unwanted
> id - var echoing.

But first of course use any of mode-switching doctypes, say
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">

Then take care of IE by pre-declaring all global variables using var
statement - which is a good programming style anyway.

Richard Cornford

unread,
Mar 3, 2007, 5:33:49 PM3/3/07
to
Křištof Želechovski <giec...@stegny.2a.pl> wrote:
> At <http://jibbering.com/faq/index.html#FAQ4_41>
> IS
> Microsoft introduced a shortcut that can be used to reference
> elements which include an ID attribute where the ID becomes a
> global variable.
> SHOULD BE
> Microsoft introduced a shortcut that can be used to reference
> elements THAT ARE NOT FORM CONTROLS WITHIN A FORM ELEMENT

In the context of question answered in the FAQ that is an inconsequential
detail. (There is no sense in telling someone about never being able to
reference a particular from of IDed element by Identifier when they are
asking why accessing IDed elements by Identifier does not work in all
environments.)

> which include an ID attribute where the ID becomes A PROPERTY
> OF THE GLOBAL WINDOW OBJECT

The only practical distinction between a global variable and a property
of the global object is that a property of the global object can be
deleted and a global variable cannot be deleted. The properties of the
global object that result form IDed elements in the document on IE cannot
be deleted.

> and the scope of the window object

Objects do not have scopes, only functions and execution contexts have
them.

> is at the bottom of the symbol lookup stack..

The global object is at the end of all scope chains. ('lookup stack' has
no meaning in ECMAScript).

> In other words, if your design for Microsoft Internet
> Explorer, you should use the syntax WINDOW.DEVID

Should you? What is wrong with using -
document.getElementById("DEVID") -, as that works fine in IE as well as
other browsers?

> to avoid name clash

You would only risk a naming clash if making the reference from an
execution context that had an object on its scope chain that had a
property named "DIVID, which would itself be symptomatic of inappropriate
code design (with regard to variable/property naming).

> and make symbol lookup more efficient.

The old wives tail that qualifying property accessors in IE with -
window - makes resolving them faster has been demonstrated false often
enough by now to make repeating it in public a poor idea.

Richard.

Randy Webb

unread,
Mar 3, 2007, 11:28:54 PM3/3/07
to
Richard Cornford said the following on 3/3/2007 5:33 PM:
> Kr(ištof Želechovski <giec...@stegny.2a.pl> wrote:

<snip>

>> In other words, if your design for Microsoft Internet
>> Explorer, you should use the syntax WINDOW.DEVID
>
> Should you? What is wrong with using - document.getElementById("DEVID")
> -, as that works fine in IE as well as other browsers?

Speed.

<snip>

>> and make symbol lookup more efficient.
>
> The old wives tail

Is it a "wives tail"? My testing tells me differently.

> that qualifying property accessors in IE with - window - makes
> resolving them faster has been demonstrated false often
> enough by now to make repeating it in public a poor idea.

You may want to re-test that with IE7.

/*
Below is just code I used to create a 1000 divs with ID's to copy/paste
into the test page.

for (i=0;i<1000;i++){
document.write('&lt;div id="myDiv' + i + '"&gt;myDiv' + i + '
content&lt;/div&gt;<br>')
}
*/

function testWindow(){
elementToGet = "myDiv" + Math.floor(1000*Math.random())
var start1 = new Date()
for (i=0;i<50000;i++){
var tempVar = window.elementToGet
}
var end1 = new Date()
var totalTime1 = end1-start1
window.results.innerHTML += 'window.ID took '+totalTime1+'milliseconds<br>'
}

function testgEBI(){
var start2 = new Date()
for (i=0;i<50000;i++){
var tempVar = document.getElementById(elementToGet)
}
var end2 = new Date()
var totalTime2 = end2-start2
window.results.innerHTML +=
'document.getElementById(ID) took '+totalTime2+' milliseconds<br>'

}


<button onclick="testWindow()">Test window.ID</button>
<button onclick="testgEBI()">Test getElementById</button>
<div id="results"></div>

<div id="myDiv0">myDiv0 content</div>
<div id="myDiv1">myDiv1 content</div>

rest of div elements here

<div id="myDiv998">myDiv998 content</div>
<div id="myDiv999">myDiv999 content</div>

I am consistently getting results in this range:
window.ID took 265 milliseconds and
document.getElementById(ID) took 22422 milliseconds

Which makes window.ID on the order of 85 times faster than gEBI in IE7.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Gérard Talbot

unread,
Mar 4, 2007, 1:05:29 AM3/4/07
to
Randy Webb wrote :
Randy, I've tried to reach Jim and yourself in order to update a few
issues in http://jibbering.com/faq/

4.41 was one item regarding my emails:

The link given in 4.41 should be updated.

http://www.mozilla.org/docs/web-developer/upgrade_2.html#dom_access
should be replaced with
http://developer.mozilla.org/en/docs/Using_Web_Standards_in_your_Web_Pages#Accessing_Elements_with_the_W3C_DOM

Email sent on February 6th 2007
cljfaq home ctvea and net
jim home jibbering and com

Will post the other issues in a distinct post/new thread.

Gérard
--
Using Web Standards in your Web Pages (Updated Dec. 2006)
http://developer.mozilla.org/en/docs/Using_Web_Standards_in_your_Web_Pages

Richard Cornford

unread,
Mar 4, 2007, 6:40:03 AM3/4/07
to
Randy Webb wrote:
> Richard Cornford said the following on 3/3/2007 5:33 PM:
>> Kr(istof Zelechovski wrote:
> <snip>
>>> In other words, if your design for Microsoft Internet
>>> Explorer, you should use the syntax WINDOW.DEVID
>>
>> Should you? What is wrong with using -
>> document.getElementById("DEVID") -, as that works fine in IE as well
>> as other browsers?
>
> Speed.

Speed is only significant if the reference is to be looked up
sufficiently frequently.

> <snip>
>
>>> and make symbol lookup more efficient.
>>
>> The old wives tail
>
> Is it a "wives tail"? My testing tells me differently.
>
>> that qualifying property accessors in IE with - window - makes
>> resolving them faster has been demonstrated false often enough by now
>> to make repeating it in public a poor idea.
>
> You may want to re-test that with IE7.

I would be very surprised if - someGlobalPropertyName - was not also
faster to look up than - window.someGlobalPropertyName - on IE 7.

<snip>


> Which makes window.ID on the order of 85 times faster
> than gEBI in IE7.

The old wives tail being repeated here was that - window.ID - is faster
to resolve than just - ID -. The relative performance of -
getElementById - is not relevant to that.

Richard.

0 new messages