Table of Contents

About

In a browser, the document is:

The document is provided by the browser via its DOM webapi (Not by the Javascript engine, DOM is not a standard javascript library)

Variable

The document variable exists as a global variable

The document variable is:

  • is a property of the window object
  • accessible globally through an alias

Example

type = document.contentType;
console.log("The document type is "+type);

Browser Detection

The presence of the document object tells you that Javascript is running in:

  • a browser
  • or a library that supports DOM such as JsDom
if (typeof document !== 'undefined') {
  // We are in a browser context
} else {
  // We are in a non-browser context (node without dom library)
}