Getting document is not defined , from document.getElementById

跟風遠走 提交于 2020-08-15 10:10:07

问题


I am learning JavaScript and I am using Atom (Text Editor). On my HTML file I got only this:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
</head>
<body>
    <h1>Hello Plunker!</h1>
    <button id="displayTodosButton">Display Todos</button>
    <button>Toggle Todos</button>
</body>
</html>

On my javascript file, I am simply trying to access the "Display todos" button using this:

var displayTodosButton = document.getElementById('displayTodosButton')

I was watching a video, and the instructor is using plnkr.co, and he accesses the button just fine, yet on Atom I get the "ReferenceError: document is not defined"

How can I fix this?


回答1:


yet on Atom I get

If you really mean that Atom, your text editor, is highlighting it and showing you a warning that document is undefined, it's just that Atom doesn't realize you're running that code in a browser context where document will be defined.

It probably has a setting where you can tell it that you'll be running the code in a browser, so it can assume the default set of globals (window, document, etc.).


If the code in script.js is just what you've shown, although the error Atom is showing you won't be a problem (because in the browser, document will not be undefined), you'll get null back from getElementById because your code runs before the element exists. Again, this is assuming that code is on its own, not (say) inside a DOMContentLoaded handler or similar.

Unless you have a good reason to do it (and there aren't many), putting script elements in the head is an anti-pattern. Put them in body, right at the end, just prior to the closing </body> tag. That way, any elements defined above them will have been created by the browser before your code runs.




回答2:


It looks like you are trying to run the JS code with the "script" package in atom (which is in a NodeJS context). What you actually want to do, is to run it in your web browser. So just open index.html in your favorite browser and see the magic :)




回答3:


You have hit some menu option or key combination which is trying to execute the JS file using Node.js.

Your code, however, is designed to run, embedded in a web page, using the APIs supplied by web browsers.

Web browsers, under those circumstances, will provide a document object. Node.js will not.

You need to open the HTML document in a web browser. The open in browser extension might be useful.

You can see any error reports using the Developer Tools that every major browser supplies.

(NB: The first error you will then encounter is explained by this question and answer).




回答4:


That's not Atom's issue. You're calling script.js right before the DOM is loaded, therefore document is undefined.

Wrap your code in script.js file with this:

$(document).ready(function() {
    $(function () {
      //Do my stuff
    });
});

or call the script.js file at the very end of your html document like here:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>Hello Plunker!</h1>
    <button id="displayTodosButton">Display Todos</button>
    <button>Toggle Todos</button>

    <script src="script.js"></script>
</body>
</html>


来源:https://stackoverflow.com/questions/41225295/getting-document-is-not-defined-from-document-getelementbyid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!