PDA

View Full Version : Passing variables from an external javascript.


Geuis
10-23-2002, 07:24 PM
I am trying to pass the value of variables declared in the external file into the main html file/javascript coding that calls the external file.

Example:

(main html file, index.html)

<html>

<script language="Javascript" src="external.js">

</script>

<body>
<input name="textBox" type="text" value = importedVar>
</body>

<html>


(external JS file, external.js)

function externFunc()
{
var importedVar = "Test Successful"
}
-------------------------------------------------

With the end result that the textBox will display the value "Test Successful" in the final html page. I am not sure how to pass the variable correctly and continue to get errors when I use the code written above. Any assistance is appreciated.

beetle
10-23-2002, 07:33 PM
No need to 'pass' anything. 'External' js files are external only in the sense that they are a separate file. When linked to a page, however, the end result is identical to including all the script on the page itself.

Answer your question?

Geuis
10-23-2002, 08:02 PM
I would only disagree because that code does not work. I get javascript errors when that code is run.

mordred
10-23-2002, 08:22 PM
*sigh*

Give us some code to play with: Which errors do you get (the JavaScript-Console in Mozilla usually provides the most informative error messages), and how and where in your code to you actually call externFunc()?

If you want to set the value of the input field, you usually have it like

document.forms["formName"].elements["fieldName"].value = "foo";

Be sure to call the above statement after the page has finished loading; at the stage where the external javascript is compiled, it has no idea of the document structure. The onload eventhandler helps you handle this issue though.

beetle
10-23-2002, 08:51 PM
Originally posted by Geuis
I would only disagree because that code does not work. I get javascript errors when that code is run. Ok, what I said is true. Period. If you are getting errors, it is because of another problem, even if it DOES involve variables, it has nothing to do with the fact that some of your javascript is 'external'