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

Best way to feature-test?

23 views
Skip to first unread message

per...@gmail.com

unread,
Jul 15, 2012, 8:50:16 PM7/15/12
to
Hi everyone,

I've been using the following technique to detect if a certain functionality is supported by a browser:


if(document.someFunctionality) {
// Use someFunctionality
}

But lately, I've been reading that "document", "window" and other browser objects are "host objects", and host objects have their own rules and behaviors that depend on the browser and the platform. So it's possible that merely testing "document.someFunctionality" can throw an exception in some browser on some platform.

So, in order to cover those cases, is it reasonable to do the following?

if(typeof document.someProperty != 'undefined') {
// Go ahead and use the property
}

if(typeof document.someFunction == 'function') {
// Go ahead and invoke "document.someFunction()"
}

Or is it overkill?


per...@gmail.com

unread,
Jul 15, 2012, 8:54:03 PM7/15/12
to
Oops, I should have titled it "Best way to feature-detect?".

Gregor Kofler

unread,
Jul 16, 2012, 4:48:37 AM7/16/12
to
Am 2012-07-16 02:50, per...@gmail.com meinte:
Quite the contrary.

Some links for further reading:

<http://www.cinsoft.net/host.html>
<http://peter.michaux.ca/articles/feature-detection-state-of-the-art-browser-scripting>


Gregor


--
http://vxweb.net

Ivan The Not-So-Bad

unread,
Jul 16, 2012, 9:49:44 AM7/16/12
to
Look into modernizr: http://modernizr.com/

Tim Down

unread,
Jul 16, 2012, 11:42:59 AM7/16/12
to
On Monday, 16 July 2012 14:49:44 UTC+1, Ivan The Not-So-Bad wrote:
> Look into modernizr: http://modernizr.com/

Take care: Modernizr includes HTML5 Shiv, which actively breaks fundamental host methods such as document.createElement() in IE < 9.

https://github.com/aFarkas/html5shiv/issues/64

Tim

Ivan The Not-So-Bad

unread,
Jul 16, 2012, 1:35:57 PM7/16/12
to
Good point, Tim.

Thanks for the head up!

Thomas 'PointedEars' Lahn

unread,
Jul 16, 2012, 3:29:49 PM7/16/12
to
Gregor Kofler wrote:

> Am 2012-07-16 02:50, per...@gmail.com meinte:
>> […] lately, I've been reading that "document", "window" and other browser
>> objects are "host objects", and host objects have their own rules and
>> behaviors that depend on the browser and the platform. So it's possible
>> that merely testing "document.someFunctionality" can throw an exception
>> in some browser on some platform.
>>
>> So, in order to cover those cases, is it reasonable to do the following?
>>
>> if(typeof document.someProperty != 'undefined') {
>> // Go ahead and use the property
>> }
>>
>> if(typeof document.someFunction == 'function') {
>> // Go ahead and invoke "document.someFunction()"
>> }
>>
>> Or is it overkill?
>
> Quite the contrary.
>
> Some links for further reading:
>
> <http://www.cinsoft.net/host.html>
> […]

I have since realized not only the usefulness of having a method that can
deal with separate object reference and property names instead of a `typeof'
result (isMethod), but also of having a separate method for methods of
host and native objects (isNativeMethod) each. (jsx.object.isMethodType(),
formerly isMethodType(), is still supported but has been marked deprecated
accordingly. It may not be supported in future revisions.)

Therefore, JSX:object.js contains jsx.object.isMethod()) (formerly
isMethod()) since at least revision 123 (2008-05-29), and
jsx.object.isHostMethod() (alias for .isMethod()) and
jsx.object.isNativeMethod() (wrapper for .isMethod()) since revision 234
(2011-12-12).

jsx.object.areNativeMethods(), jsx.object.areHostMethods(), and
jsx.object.areMethods(), the latter of which has been been introduced in
revision 146 (2009-07-16), are aliases for those methods, respectively. As
an extension to the methods in David's library (so to speak), they also
support calls of the form

if (jsx.object.isHostMethod(window, "document", "write"))

(accepts an arbitrary number of arguments) which prevents you from having to
write

if (typeof window.document != "undefined"
&& jsx.object.isHostMethod(window.document, "write"))

and

if (jsx.object.areHostMethods(document, ["open", "write", "close"]))

which prevents you from having to write

var _isHostMethod = jsx.object.isHostMethod;
if (_isHostMethod(document, "open")
&& _isHostMethod(document, "write")
&& _isHostMethod(document, "close"))

which is not only tedious to write but also inefficient by comparison.

See
<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fobject.js>
for more.


PointedEars
--
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
-- Richard Cornford, comp.lang.javascript, 2011-11-14

Jim T.

unread,
Jul 18, 2012, 1:22:06 PM7/18/12
to
On Sun, 15 Jul 2012 17:50:16 -0700 (PDT), per...@gmail.com wrote:

>Hi everyone,
>
>I've been using the following technique to detect if a certain functionality is supported by a browser:
>
>
>if(document.someFunctionality) {
> // Use someFunctionality
>}
>
>But lately, I've been reading that "document", "window" and other browser objects are "host objects", and host objects have their own rules and behaviors that depend on the browser and the platform. So it's possible that merely testing "document.someFunctionality" can throw an exception in some browser on some platform.

Are there any actual browsers/platforms out there now where this test
fails?

Jim T.

unread,
Jul 18, 2012, 1:24:43 PM7/18/12
to
On Wed, 18 Jul 2012 13:22:06 -0400, Jim T. <x@y.z> wrote:

>On Sun, 15 Jul 2012 17:50:16 -0700 (PDT), per...@gmail.com wrote:
>
>>Hi everyone,
>>
>>I've been using the following technique to detect if a certain functionality is supported by a browser:
>>
>>
>>if(document.someFunctionality) {
>> // Use someFunctionality
>>}
>>
>>But lately, I've been reading that "document", "window" and other browser objects are "host objects", and host objects have their own rules and behaviors that depend on the browser and the platform. So it's possible that merely testing "document.someFunctionality" can throw an exception in some browser on some platform.
>
>Are there any actual browsers/platforms out there now where this test
>fails?

Never mind, I just read Gregor's post. I've got some fixin' to do...
0 new messages