Browser - Document variable (DOM) - Javascript


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)


The document variable exists as a global variable

The document variable is:

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


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)

Powered by ComboStrap