Search code examples
javascriptvalidationmyeclipse

How can I get my JavaScript, referencing a var defined externally, to validate?


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.


Solution

  • 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:

    /*global getElementByAttribute, breakCycles, hanoi */