Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #15063 > unrolled thread

Best way to feature-test?

Started byperryv@gmail.com
First post2012-07-15 17:50 -0700
Last post2012-07-18 13:24 -0400
Articles 9 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  Best way to feature-test? perryv@gmail.com - 2012-07-15 17:50 -0700
    Re: Best way to feature-test? perryv@gmail.com - 2012-07-15 17:54 -0700
    Re: Best way to feature-test? Gregor Kofler <usenet@gregorkofler.com> - 2012-07-16 10:48 +0200
      Re: Best way to feature-test? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-07-16 21:29 +0200
    Re: Best way to feature-test? Ivan The Not-So-Bad <1suf41n@yahoo.com> - 2012-07-16 13:49 +0000
      Re: Best way to feature-test? Tim Down <timdown@gmail.com> - 2012-07-16 08:42 -0700
        Re: Best way to feature-test? Ivan The Not-So-Bad <1suf41n@yahoo.com> - 2012-07-16 17:35 +0000
    Re: Best way to feature-test? Jim T. <x@y.z> - 2012-07-18 13:22 -0400
      Re: Best way to feature-test? Jim T. <x@y.z> - 2012-07-18 13:24 -0400

#15063 — Best way to feature-test?

Fromperryv@gmail.com
Date2012-07-15 17:50 -0700
SubjectBest way to feature-test?
Message-ID<9279c95c-914c-4a92-aa23-495f81bccc1a@googlegroups.com>
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?

[toc] | [next] | [standalone]


#15064

Fromperryv@gmail.com
Date2012-07-15 17:54 -0700
Message-ID<a76806da-13d4-4e66-8a5e-7f40b96970ae@googlegroups.com>
In reply to#15063
Oops, I should have titled it "Best way to feature-detect?".

[toc] | [prev] | [next] | [standalone]


#15065

FromGregor Kofler <usenet@gregorkofler.com>
Date2012-07-16 10:48 +0200
Message-ID<ju0kh6$itl$1@dont-email.me>
In reply to#15063
Am 2012-07-16 02:50, perryv@gmail.com meinte:
> 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?

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

[toc] | [prev] | [next] | [standalone]


#15079

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-07-16 21:29 +0200
Message-ID<2169875.p79QOsc7RG@PointedEars.de>
In reply to#15065
Gregor Kofler wrote:

> Am 2012-07-16 02:50, perryv@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

[toc] | [prev] | [next] | [standalone]


#15068

FromIvan The Not-So-Bad <1suf41n@yahoo.com>
Date2012-07-16 13:49 +0000
Message-ID<50041bf8$0$11649$c3e8da3$dbd57e7@news.astraweb.com>
In reply to#15063
On 2012-07-16, perryv@gmail.com <perryv@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.
>
> 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?
>
>

Look into modernizr: http://modernizr.com/

[toc] | [prev] | [next] | [standalone]


#15070

FromTim Down <timdown@gmail.com>
Date2012-07-16 08:42 -0700
Message-ID<24b91f4f-8b46-4879-93d1-69ddb9ba9e49@googlegroups.com>
In reply to#15068
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

[toc] | [prev] | [next] | [standalone]


#15074

FromIvan The Not-So-Bad <1suf41n@yahoo.com>
Date2012-07-16 17:35 +0000
Message-ID<500450fd$0$1583$c3e8da3$92d0a893@news.astraweb.com>
In reply to#15070
On 2012-07-16, Tim Down <timdown@gmail.com> wrote:
> 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

Good point, Tim.

Thanks for the head up!

[toc] | [prev] | [next] | [standalone]


#15094

FromJim T. <x@y.z>
Date2012-07-18 13:22 -0400
Message-ID<qvrd08hanlqao2jr4rvpe9u7maj3lr7rd1@4ax.com>
In reply to#15063
On Sun, 15 Jul 2012 17:50:16 -0700 (PDT), perryv@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? 

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

[toc] | [prev] | [next] | [standalone]


#15095

FromJim T. <x@y.z>
Date2012-07-18 13:24 -0400
Message-ID<b9sd08pmnckh132g26q1truu8t8q0pheej@4ax.com>
In reply to#15094
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), perryv@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...

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web