kangax said:
Garrett said:
Thomas 'PointedEars' Lahn wrote: [...]
You are mistaken because alert() is a host method (of Window objects,
and
should therefore be called window.alert()). It may display anything
there.
The alert method converts its argument to a string value.
It's surprising that neither MDC, nor MSDN nor HTML5 draft mention
anything about `alert` performing such conversion.
MDC is not perfect.
MSDN is a crapshoot. Some of the documentation is good. However, a lot
it is pretty poor.
HTML5 - I'll post that on the list.
Yes. Some mobile clients have very diverse function representations. I
actually had similar concerns about function decompilation and wrote
about a more reliable way to detect built-in host methods [1].
Unfortunately, my solution is not perfect either, since it relies on
iframe insertion into a document.
Which ones?
You might also want to let PPK know about that.
Your article says you are testing for something in a "non-modified"
context. I assume this means that the context is the newly created
iframe's window object is un-buggered.
Why not just not give the global object an addEventListener property?
Oh, maybe because "somebody might have done that."
In that case that same somebody might not have used standards mode
(doctype) and the document created in the iframe might use standards
mode. Or vice versa (hopefully not that(!)).
You also mention speed. The iframe will also have to be garbage
collected and that will slow things down a bit, too.
There is one other consideration. A pretty serious one, too. In IE,
appending a child mid-parse will result in a lovely "operation aborted"
error[1][2]. This makes the page stop rendering and basically kills your
webapp. So: If your blog is "perfection kills", then I guess you could
call this strategy of appending a node to body while it is being parsed
"perfection".
j/k
So this:-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"
http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Detecting built-in host methods</title>
</head>
<body>
<div>
<script type="text/javascript">
var isNativeWindowAddEventListenerPresent = (function(global){
// feature test following methods as needed
var el = document.createElement('iframe');
var root = document.body || document.documentElement;
root.appendChild(el);
var frame = global.frames[global.frames.length-1];
var result = (typeof frame.addEventListener != "undefined");
//root.removeChild(el);
el = null;
return result;
})(this);
</script></div>
</body>
</html>
would result in "operation aborted" error in IE.
The example, as it is written on your blog, gets away with it when the
root.removeChild line is uncommented (as it appears there). It also
works when you don't put the script in a div (which I purposely added to
trigger the error in IE). There should not be a good reason to have that
div anyway. The problem is avoidable, in most cases.
When adding test nodes to body while it is possibly being parsed, I like
use insertBefore(body.firstChild, testEl) instead of appendChild. I have
not had a problem with that yet. I think D Mark and I discussed this
technique about a year ago in "Find Element Position" thread (though I
may be mistaken; we did exchange a good amount of email during that time).
Garrett
[1]
http://support.microsoft.com/kb/927917
[2]
http://blogs.msdn.com/ie/archive/2008/04/23/what-happened-to-operation-aborted.aspx