From: Thomas 'PointedEars' Lahn on
Thomas 'PointedEars' Lahn wrote:

> Garrett Smith wrote:
>> Calling a host method with a different base object won't work in most
>> browsers.
>
> I believe I have said that.

Essentially.

>> [preaching to the choir]
>> However it *will* work in most IE versions.
>
> It sure did not work properly back then. Perhaps it was not MSHTML or it
> was another method who was the culprit.

There, I knew it (I cannot remember ever having written code without good
reason):

--- dhtml-commented.js ---

if (jsx_object.isMethod(document, "getElementById"))
{
/**
* @param s : string
* @return Element|null
*/
return function(s) {
/* wrapper method required to avoid "invalid op. on prototype"
exception */
return document.getElementById(s);
};
}

---

That exception is thrown in *Mozilla*-based browsers, by the Gecko DOM, when
doing as Stefan suggested. Turns out that it applies one way or the other
(type of exception differs) to *all* known DOMs *except* that of MSHTML
(with regard to the versions of it that I have tested so far), which is, at
least in my book, far worse.

> Anyhow, it was bad advice to be corrected.

AISB.


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)
From: Garrett Smith on
On 2010-06-28 05:07 PM, Thomas 'PointedEars' Lahn wrote:
> Garrett Smith wrote:
>
>> Thomas 'PointedEars' Lahn wrote:
>>> RobG wrote:
>>>> The code in the article has a $() function that simply calls
>>>> document.getElementById() and is used precisely once. So to save
>>>> typing 22 characters, the author wrote a function of 53 characters
>>>> and spaces (plus a bunch more for the comment). Consider using
>>>> instead:
>>>>
>>>> var $ = document.getElementById;
>>>>
>>>> within the scope it is required. No global "$", no extra function
>>>> call, far fewer characters to transmit and exactly (more or less) the
>>>> same functionality.
>>>
>>> It does not work as you suggest because the calling object would not be
>>> that referred to by `document' anymore, but the Global Object. As a
>>> result, at least MSHTML would throw an exception. I had to observe that
>>> when writing JSX:dhtml.js years ago and I presume it still holds true.
>>> So a wrapper is required if you want an alias for this, but it should
>>> have a better, self-descriptive identifier (like `dom.getEBI' etc. in
>>> JSX:dhtml.js). Since then this method included also feature testing for
>>> the MSHTML 4 and NS 4 DOMs, but it would appear that these can either be
>>> safely discarded in the near future or moved to compatibility libraries.
>>
>> I see your case, but I have a few disagreements.
>
> Well, you are rather prone to misconceptions.
>

Aside from the typo that Stephen Weiss caught (thanks), did I post
something wrong? Seems not.

You referred to "base object" as "calling object" and called that global
object. That's wrong terminology. If the aforementioned code been
defined in global context, then yes, the base object would be the global
object, but I disagree with calling that a "calling object" because it
is not the correct terminology.

In all IE versions so far, `document.getElementById` is a callable host
object that is not actually a function. There isn't any spec that states
that the thisArg matters for these methods. Does it matter? Maybe? Is it
safe to assume it doesn't? Of course not! -- And I never have suggested
otherwise.

http://ecma262-5.com/ELS5_HTML_with_CorrectionNotes.htm#Section_11.2.3

>> Calling a host method with a different base object won't work in most
>> browsers.
>
> I believe I have said that.
>
>> [preaching to the choir]

I did not write that.

>> However it *will* work in most IE versions.
>
> It sure did not work properly back then. Perhaps it was not MSHTML or it
> was another method who was the culprit. Anyhow, it was bad advice to be
> corrected.
>
>> This is old news, appearing in an old post of erik's weblog: "appendChild
>> is not a function" and
>

http://erik.eae.net/archives/2006/04/27/17.50.15/

Whether or not Erik was wrong about other points doesn't really matter.
That entry is the one that counts here.

> You don't mean the guy that completely botched JScript/MSHTML, do you?
>

Dunno about that, but the entry itself points out the fact I stated.
it's quoted so search google for that string and you'll see.

Here...

>> restated as recently as two weeks ago wherein it was mentioned that MSIE
>> host methods tend to retain a reference to the base object.
>
> It is an unnecessary risk to take, especially considering a proprietary,
> closed source implementation; a sure sign of a script-kiddie. Why am I not
> surprised to see you recommending that?
>

What a load of bs. First off, I never ever recommended doing that; I
recommended against doing that both here and in that other thread.

In "Need help with setTimeout", Gregor recommended:

| window.setTimeout(mf.reset, 10000);
|
| is what you want.

And I replied to explain why that can't be expected to work reliably.

| Passing the value of `mf.reset` to setTimeout will cause the function
| to execute in 10000 ms with a null base object. That cannot be
| expected to work reliably.
|
| It will submit the form in most versions of IE, but will result in an
| error in other implementations. MSIE is a notable exception, as its
| host objects tend to retain a reference to their base object.

So what I did was I pointed out that what you stated "doesn't work in
IE" was not true. I never recommended such things.

I'd appreciate if you'd not make such false statements about me.

The mistake I made in the other thread was not helping the OP enough. I
deliberately decided not give away too much, so as to make him think
about it. I think I misjudged and did not reveal enough.

Garrett
From: Garrett Smith on
On 2010-06-28 05:30 PM, Thomas 'PointedEars' Lahn wrote:
> Thomas 'PointedEars' Lahn wrote:
>
>> Garrett Smith wrote:
>>> Calling a host method with a different base object won't work in most
>>> browsers.
>>
>> I believe I have said that.
>
> Essentially.
>

It appears that you are conversing with yourself.

>>> [preaching to the choir]
>>> However it *will* work in most IE versions.
>>
>> It sure did not work properly back then. Perhaps it was not MSHTML or it
>> was another method who was the culprit.
>
> There, I knew it (I cannot remember ever having written code without good
> reason):
>

Knew what? That the base object matters in most browsers?

> --- dhtml-commented.js ---
>
> if (jsx_object.isMethod(document, "getElementById"))
> {
> /**
> * @param s : string
> * @return Element|null
> */
> return function(s) {
> /* wrapper method required to avoid "invalid op. on prototype"
> exception */
> return document.getElementById(s);
> };
> }

What do you return if document.getElementById is not available?

>
> ---
>
> That exception is thrown in *Mozilla*-based browsers, by the Gecko DOM, when

Forget about "Mozilla" for a second. Think bigger. The exception is
thrown in any browser where the thisArg to the getElementById function
matters. That is likely to happen in any browser that uses DOM
prototypes. Gecko happens to be one such browser.

> doing as Stefan suggested. Turns out that it applies one way or the other
> (type of exception differs) to *all* known DOMs *except* that of MSHTML
> (with regard to the versions of it that I have tested so far), which is, at
> least in my book, far worse.
>
>> Anyhow, it was bad advice to be corrected.
>

It's been done and now you're repeating what I wrote earlier.

Again, I did not recommend using that approach. I'm fairly keen on how
function calls work, how DOM prototypes work, and IE host objects, as
they pertain to web page scripting. I recommended *agaist* using that
approach whenever I see it because it doesn't work in IE.

Please don't suggest that I recommended doing that.

Garrett
From: Garrett Smith on
On 2010-06-28 06:26 PM, Garrett Smith wrote:
> I recommended *agaist* using that approach whenever I see it because it
> doesn't work in IE.

Correction: Because it doesn't work cross-browser.

Garrett
From: Thomas 'PointedEars' Lahn on
Garrett Smith wrote:

> On 2010-06-28 05:30 PM, Thomas 'PointedEars' Lahn wrote:
>> Thomas 'PointedEars' Lahn wrote:
>>> Garrett Smith wrote:
>>>> Calling a host method with a different base object won't work in most
>>>> browsers.
>>> I believe I have said that.
>> Essentially.
>
> It appears that you are conversing with yourself.

It appears that you are not paying attention.

>>>> [preaching to the choir]
>>>> However it *will* work in most IE versions.
>>> It sure did not work properly back then. Perhaps it was not MSHTML or
>>> it was another method who was the culprit.
>> There, I knew it (I cannot remember ever having written code without good
>> reason):
>
> Knew what? That the base object matters in most browsers?

No, that I had a reason for not using a simple alias as suggested by
_Stefan_.

>> --- dhtml-commented.js ---
>>
>> if (jsx_object.isMethod(document, "getElementById"))
>> {
>> /**
>> * @param s : string
>> * @return Element|null
>> */
>> return function(s) {
>> /* wrapper method required to avoid "invalid op. on
>> prototype"
>> exception */
>> return document.getElementById(s);
>> };
>> }
>
> What do you return if document.getElementById is not available?

Depends on the remaining alternatives. Learn to read.

>> That exception is thrown in *Mozilla*-based browsers, by the Gecko DOM,
>> when
>
> Forget about "Mozilla" for a second. Think bigger.

You are not paying attention. Here I have been referring to the main reason
why I wrote dhtml.js the way I have written it.

> The exception is thrown in any browser where the thisArg to the
> getElementById function matters. That is likely to happen in any browser
> that uses DOM prototypes.

You don't say!

> Gecko happens to be one such browser.

Gecko is not a browser.

>> doing as Stefan suggested. Turns out that it applies one way or the
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> other (type of exception differs) to *all* known DOMs *except* that of
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> MSHTML (with regard to the versions of it that I have tested so far),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> which is, at least in my book, far worse.


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)