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 8198275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:55:03+00:00 2026-06-07T05:55:03+00:00

What is the best and safest (not shortest/fastest) solution for (cross-browser/-platform) checking the existence

  • 0

What is the best and safest (not shortest/fastest) solution for (cross-browser/-platform) checking the existence of the document object (and maybe the window object if necessary)?

(function(root, undefined) {

    var document = false;

    if(typeof root.document === "object" && root.document !== null)
        document = root.document;

    // ...

    if(document !== false)
        doMyFancyClientSideStuff();

})(this);

Is typeof window.document in browsers always "object"? Or is there maybe anything like a work-around needed to make sure that our received object is really the DOM type of object, like window.document instanceof window.Document, and not just a self made object or what else.

  • 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-06-07T05:55:03+00:00Added an answer on June 7, 2026 at 5:55 am

    Your definition of “Cross-Browser” is pretty cruicial here. I’d actually recommend to check for the [[Class]] property like:

    Object.prototype.toString.call( root.document );
    

    That should return [object HTMLDocument] in modern’ish browsers (or [object Undefined]). To also include IE8+ on this list, we need to call

    if( "" + this.document === '[object HTMLDocument]' ) { }
    

    This would create a list of

    • IE8+
    • Chrome
    • Firefox
    • Safari
    • Opera (I don’t know, but think so)

    IE<8 will always return [object] on the window.document [[Class]]. This is at least, the most accurate check I can think of. You can of course, also just check something like

    if( 'document' in this ) { }
    

    but that is no guarantee that we are talking about an actuall DOM object, just that there is a property called document in the global object.

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

Sidebar

Related Questions

best solution for use access in project for database with security i need to
In SQL Server 2008 and higher what is the best/safest/most correct way to retrieve
What's the safest and best way to retrieve an unsigned long from a string
Best, smallest, fastest, open source, C/C++ 3d renderer (with support of 3ds max models),
I am looking for the best and safest way to convert a git branch
Best practices question: Is there anything improper about creating a new object within another
Best practices aside, if I create an object that is owned by my UIApplicationDelegate
I'm wondering wich is best solution for maintaining huge amount of small TCP connections
What's safest and best method to move $config['encryption_key'] out of CodeIgniter so it can't
Wich is the best (and safest) way to implement user login for a silverlight

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.