最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

adobe brackets - How to define document in JavaScript - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Mar 4, 2018 at 13:17 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 3, 2018 at 19:11 JamesbondJamesbond 211 gold badge1 silver badge2 bronze badges 3
  • 1 "Assume a browser?" i.sstatic/ch78U.png – Matt Ball Commented Jan 3, 2018 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 Commented Jan 3, 2018 at 19:13
  • 1 In addition to @tadman above, it's document.getElementById, not document.getElemenstById because ids should be unique within a document, so you should never find two with the same id. – Scott Marcus Commented Jan 3, 2018 at 19:23
Add a ment  | 

2 Answers 2

Reset to default 3

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 mand 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.

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.

发布评论

评论列表(0)

  1. 暂无评论