Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6851431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:14:49+00:00 2026-05-27T01:14:49+00:00

I would like advice as to best practice in testing object existence for cross-browser

  • 0

I would like advice as to best practice in testing object existence for cross-browser compatibility.

There seem to be many ways of testing for object/function/attribute existence. I could use jquery or another library, but for now I want to stick as closely as possible to w3c rather than use what amounts to a whole new language.

What I’m trying to do

I’m trying to write a utility library that tries to stick to w3c methods so I can just call

xAddEventListener(elem, type, listener, useCapture)

for all browsers rather than

elem.AddEventListener(type, listener, useCapture)

just for w3c compliant browsers. If another library already does this, please let me know.

I saw this today:

if (typeof node.addEventListener == "function")

but will this ever yield a different result than plain

if (node.addEventListener)

Style documents?

A reference to a standards or styles document would also be useful. I’ve found https://developer.mozilla.org/en/Browser_Detection_and_Cross_Browser_Support
but that was last updated in 2003. It advocates simple

if (document.images)

tests for most existence tests and

if (typeof(window.innerHeight) == 'number')

only with numbers because if(0) would evaluate to false

Examples to inspire comment:

if (myObject)

Can an object or function ever fail this simple test?

if (myObject != undefined)

When is this better than the previous test?

if (typeof(myObject) == 'object')

This appears to be the original way of calling type of, but some people say that typeof is a keyword and not a function. Also, why not one of the simpler tests?

if ( typeof myObject.function !== undefined ) {

One post said to alway use === or !== as it differentiates between null and undefined. Is this ever important in practice?

Another possibility is:

try {
    node.addEventListener(...)
} 
catch(err) {
    node.attachEvent(...)
}

Which in python appears to be becoming the favourite way of dealing with these type of things.

Using exceptions looks potentially much cleaner as you could write easy to understand w3c compliant code, and then deal with exceptions when they come.

Anyway, what do people think? Please can you list the pros and cons of methods you like/dislike, rather than simply advocating your favourite.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:14:50+00:00Added an answer on May 27, 2026 at 1:14 am

    It all depends on how specific you want to be / how much you want to assert before calling a function.


    if (myObject)
    

    Can an object or function ever fail this simple test?

    No, the only values that do not pass an if clause are false, 0, "", NaN, undefined and null. These are all primitives. Objects (including functions) will always pass an if clause.


    if (myObject != undefined)
    

    When is this better than the previous test?

    If you want to check whether a value is "meaningful", i.e. not undefined or null. For example,

    if(numberInputtedByUser) {
        // do something with inputted number
    }
    

    will fail the if clause if the number is 0, while you probably want 0 to be allowed. In such case, != undefined is a slightly better check.


    if (typeof(myObject) == 'object')
    

    This appears to be the original way of calling type of, but some people say that typeof is a keyword and not a function. Also, why not one of the simpler tests?

    It is a keyword. You can call it in a function-like fashion, though. In its most bare form you can use typeof like this:

    typeof myObject
    

    You can, however, add (extraneous) parens since they don’t mean anything:

    typeof (myObject)
    

    Just like you can do:

    (myObject).key
    

    or even:

    (((myObject))).key
    

    And you can then remove the space after the typeof if you want, resulting in something that looks like a function call.

    As to why to use typeof – you can be even more certain of the type of variable. With the if(...) test, the values that pass can be all kind of things – basically everything except the list I posted above. With if(... != undefined), you allow even more to be passed. With if(typeof ... == 'object'), you really only allow objects which might be necessary depending on what you’re processing.


    if ( typeof myObject.function !== undefined ) {
    

    One post said to alway use === or !== as it differentiates between null and undefined. Is this ever important in practice?

    === is really preferred over ==. While differentiating between null and undefined is not always necessary, it is a very good practice to save yourself from the results of quirks like 0 == ''. If you want to check whether a number is 0, then === 0 is the way to go, since == 0 also allows for an empty string (which you might not expect and probably don’t want). Even in cases == doesn’t cause quirks, you’d be better off using === at all times for consistency and avoiding surprising bugs.


    try {
        node.addEventListener(...)
    } 
    catch(err) {
        node.attachEvent(...)
    }
    

    This is of course possible and very straight-forward. Note however that try catch is said to be slow. Moreover, you don’t really account for why it fails. It’s a bit simple-minded (but may work fine).


    if (typeof node.addEventListener == "function")
    

    but will this ever yield a different result than plain

    if (node.addEventListener)
    

    Yes, like I said above, the first only passes functions whilst the second allows anything except that list of "falsy" values. One could add Node.addEventListener = 123, and it will pass the if clause in the second case. But IE fails to give a correct typeof result:

    typeof alert !== "function"
    

    I bet the same goes for addEventListener so you’d still be avoiding that function even if it exists.


    In the end, I would just use a simple if clause. Of course this will fail when you add weird things like Node.addEventListener = 123, but then again you’re bound to expect weird things happen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like some advice on the best approach to use in the following
I would like your advice about how best to solve my problem. In a
Our company is currently using a simple trunk/release/hotfixes branching model and would like advice
I am designing RESTful Api's and would like some advice on designing an API
I am considering starting an OSS project and would like some advice. I would
I'm looking into building a distributed application and would like some advice on the
I would like to hear other people's advice on when one should build a
Pretty simple question. Its just some advice based on personal opinion and best practice.
Hey all, I would like some advice from the programming community. I am building
Just want to get some advice on what is the best practice to share

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.