如何让我的 JavaScript 引用外部定义的 var 来进行验证?
我有一个包含几个外部 JavaScript 文件的 HTML 文件。 下面是一个示例,对真实的 HTML 和 JavaScript 文件进行了一些简化:
<head>
<title>Control Page</title>
<script language="JavaScript" src="control.js"></script>
<script language="JavaScript">
var myWindow;
var controlForm;
function onPageLoad() {
myWindow = document.getElementById('iframe1');
controlForm = document.getElementById('ControlForm');
}
</script>
</head>
<body onLoad="onPageLoad()">
....
</body>
</html>
然后在我的 JavaScript 文件 control.js
中:
function messageArrival(message) {
chatwindow.contentWindow.document.write(message)
}
function makeNetMeetingCall() {
controlForm.Status.value = ....
}
....
我的问题:当我验证外部 JavaScript 文件时,它会抱怨定义的变量在主 HTML 文件中,因为它们没有在 *.js
文件中的任何位置声明。 例如,MyEclipse 的 JavaScript 编辑器抱怨它看到所使用的变量未在任何可见范围内定义。 如何在 JavaScript 文件中声明这些变量,以便明确这些变量应该在外部定义,类似于 C 中的“extern
”。
I have an HTML file with a couple of external JavaScript files. Here's an example, somewhat simplified from the real HTML and JavaScript files:
<head>
<title>Control Page</title>
<script language="JavaScript" src="control.js"></script>
<script language="JavaScript">
var myWindow;
var controlForm;
function onPageLoad() {
myWindow = document.getElementById('iframe1');
controlForm = document.getElementById('ControlForm');
}
</script>
</head>
<body onLoad="onPageLoad()">
....
</body>
</html>
and then in my JavaScript file control.js
:
function messageArrival(message) {
chatwindow.contentWindow.document.write(message)
}
function makeNetMeetingCall() {
controlForm.Status.value = ....
}
....
My question: When I validate the external JavaScript file, it complains about the variables that are defined in the main HTML file because they aren't declared anywhere in the *.js
file. For example, MyEclipse's JavaScript editor complains that it sees variables used that are not defined in any visible scope. How can I declare these variables in the JavaScript file so that it's clear the variables are expected to be defined externally, something similar to "extern
" in C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要在包含controls.js 脚本之前声明变量:
这应该会阻止 Eclipse 抱怨,但执行将是相同的。
You need to declare the variables before the controls.js script is included:
That should stop eclipse from complaining, but the execution will be the same.
这听起来更像是 MyEclipse 的 JS 编辑器的问题,而不是你的 JS 的问题。
不幸的是,我对MyEclipse不熟悉。 不过,我确实知道一个很好的 JS 验证器 JSLint,它通过让您定义全局变量来解释它们在评论中:
This sounds more like an issue with MyEclipse's JS editor than your JS.
Unfortunately, I'm not familiar with MyEclipse. However, I do know of a good JS validator, JSLint, which accounts for global variables by having you define them in the comments:
您可以在 control.js 文件的全局范围内声明变量。
这不会干扰最终定义这些对象的代码,因为它首先执行。 但即使没有,如果没有初始化程序,它也不会覆盖任何内容。
如果您需要有关覆盖的证据,只需在 JS shell 中执行以下操作:
You could declare the variables in the global scope of your control.js file.
this won't interfere with the code that finally defines these objects, as it executes first. But even if it didn't, without an initializer it wouldn't overwrite anything.
If you need proof about the overwriting, just did this in the JS shell:
事实上,问题是你的“IDE”不知道它们。 它没有将 HTML 页面 JS 变量与外部 javascript 文件连接起来。 解决这个问题的一种方法是将变量声明放入外部 javascript 文件中。
In reality, the problem is that your "IDE" isn't aware of them. It hasn't connected the HTML page JS variables with the external javascript file. One way around this, is to put the variable declaration into the external javascript file.