Javascript:未定义“窗口”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14164505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:52:23  来源:igfitidea点击:

Javascript: 'window' is not defined

javascriptdom

提问by user1288851

I'm trying to learn JavaScript, but the following code has been giving me a lot of trouble:

我正在尝试学习 JavaScript,但以下代码给我带来了很多麻烦:

window.onload = function () {
    for ( var i = 0; i < seats.length; i++) {
        for ( var j = 0; j < seats.length; j++) {
            document.getElementById(getSeatId(i, j)).onclick = function(evt) {
                getSeatStatus(getSeatId(i, j));
            };
        }
    }
    document.getElementById("search").onclick = findSeat;
    document.getElementById("male_search").onclick = findMaleSeats;
    initSeats();
};

It is from an external JS file and it is the only file linked to the page. findSeat, findMaleSeats, getSeatId, and initSeatsare all defined a little bit later in the file. When I double click this file, I get the following error:

它来自外部 JS 文件,并且是唯一链接到页面的文件。findSeatfindMaleSeatsgetSeatIdinitSeats都稍后在文件中定义。当我双击此文件时,出现以下错误:

Windows Script Host
Error: 'window' is not defined
Code: 800A1391
Windows Script Host
Error: 'window' is not defined
Code: 800A1391

I already tried moving the code to other places in the file, assigning a different function (even an empty function) to window.onloadand many other things. It just seems that my computer doesn't know what windowis. And if I try to open the HTML in a browser the nothing loads (as one would expect).

我已经尝试将代码移动到文件中的其他位置,分配不同的函数(甚至是空函数)window.onload和许多其他东西。似乎我的电脑不知道是什么window。如果我尝试在浏览器中打开 HTML,则不会加载任何内容(正如人们所期望的那样)。

Does someone know what is wrong with this?

有人知道这有什么问题吗?

回答by Ayman Farhat

The window object represents an open window in a browser. Since you are not running your code within a browser, but via Windows Script Host, the interpreter won't be able to find the window object, since it does not exist, since you're not within a web browser.

window 对象代表浏览器中打开的窗口。由于您不是在浏览器中运行代码,而是通过 Windows Script Host,解释器将无法找到 window 对象,因为它不存在,因为您不在 Web 浏览器中。

回答by svidgen

It is from an external js file and it is the only file linked to the page.

它来自外部 js 文件,它是唯一链接到页面的文件。

OK.

好的。

When I double click this file I get the following error

当我双击此文件时,出现以下错误

Sounds like you're double-clicking/running a .js file, which will attempt to run the script outside the browser, like a command line script. And that would explain this error:

听起来您正在双击/运行 .js 文件,该文件将尝试在浏览器外运行脚本,就像命令行脚本一样。这将解释这个错误:

Windows Script Host Error: 'window' is not defined Code: 800A1391

Windows Script Host Error: 'window' is not defined Code: 800A1391

... not an error you'll see in a browser. And of course, the browser is what supplies the windowobject.

...不是您会在浏览器中看到的错误。当然,浏览器是提供window对象的原因。

ADDENDUM: As a course of action, I'd suggest opening the relevant HTML fileand taking a peek at the console. If you don't see anything there, it's likely your window.onloaddefinition is simply being hit afterthe browser fires the window.onloadevent.

附录:作为一个行动方案,我建议打开相关的HTML 文件并查看控制台。如果你什么也没看到有,很可能你的window.onload定义是简单地被击中的浏览器触发该window.onload事件。