views:

361

answers:

4

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.

A: 

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.

Jordan S. Jones
A: 

You need to declare the variables before the controls.js script is included:

<script language="JavaScript">
var myWindow;
var controlForm;
function onPageLoad() {
   myWindow     = document.getElementById('iframe1');
   controlForm  = document.getElementById('ControlForm');
}
</script>
<script language="JavaScript" src="control.js"></script>

That should stop eclipse from complaining, but the execution will be the same.

karim79
This wouldn't help me in my case, because the validation of the external JS file is what is failing. Yes, what you describe would be a better way to define these variables for the HTML file's parsing. That's a good idea.
Eddie
+4  A: 

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 */
Daniel Lew
What do you know? This worked! I haven't seen this kind of thing documented anywhere, so I never know about this. Thanks!
Eddie
Wait, do you mean the JSLint syntax worked in MyEclipse? I wonder if ME is using JSlint as its validator!
Daniel Lew
Yes, I mean the JSLint syntax worked in MyEclipse. I notice that I had to have EXACTLY /*g -- if I put a blank between the /* and g then it didn't work.
Eddie
+1  A: 

You could declare the variables in the global scope of your control.js file.

var controlForm;
function makeNetMeetingCall() {
 // ...
}

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:

$ js
js> var x = 1;
js> print( x );
1
js> var x;
js> print( x );
1
js>
Tim Whitlock
You can multiply (once per file) declare JS variables without replacing them? I wasn't sure what JavaScript would do with variables in the global scope of multiple files all loaded into the same scope, and I didn't know if any standard addressed this to ensure it kept working with future browsers.
Eddie
best practice would be to actually know when your application will first encounter a variable and ensure to declare it first.
Tim Whitlock