Search code examples
javascriptadobe-brackets

How to define document in JavaScript


I am using brackets editor for code editing. I have already installed a brackets extension "js lint". For DOM element I have to write document.getElemenstById or other DOM syntax. JS lint shows an error "document is not defined". So please tell me what is the syntax for define document in JS?


Solution

  • JavaScript is a language with its own syntax and built in types. document is not defined in the JavaScript (also more formally known as ECMAScript) specification. document is provided as an object API by the JavaScript "host" environment, which in a web page scenario, is the user agent (also known as the browser). Modern browser's supply a wide range of objects to the JavaScript runtime and your linter is not likely to advise you about them. However, there is a standard for the document API called the Document Object Model, which is maintained by the World Wide Web Consortium (W3C), who sets many standards for the web.

    To make a little more sense out of this, there is a very popular server-side application called Node JS. Node contains a full JavaScript runtime that conforms to the ECMAScript standard. But Node is a command line, server-side application, not a browser. You don't execute web pages in Node, so there would never be a need for a document object. Thankfully, JavaScript doesn't define a document object, so all is well.