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?
-
1"Assume a browser?" https://i.stack.imgur.com/ch78U.png – Matt Ball Jan 03 '18 at 19:13
-
4`document` is something provided by the browser runtime environment, so unless your linter has a mode that accounts for that it won't be defined. – tadman Jan 03 '18 at 19:13
-
1In addition to @tadman above, it's `document.getElementById`, not `document.getElemenstById` because `id`s should be unique within a document, so you should never find two with the same `id`. – Scott Marcus Jan 03 '18 at 19:23
2 Answers
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.

- 64,069
- 6
- 49
- 71
A quick way to remove all the JS Lint markers for "document is not defined" is to put "var document;" at the top of your JS file, just so you can see more clearly what the real errors are.
Do not leave "var document;" within the JS file though as it can cause problems.
I hope that makes things easier for you.

- 374
- 1
- 7
- 17