Forum Moderators: open

Message Too Old, No Replies

Need help with Javascript

declaring variables and then calling javascript file

         

wfernley

5:42 pm on Feb 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey everyone,

I was curious how I can create a javascript that I declare variables in and then look to an external script to use these variables.

So basically... I declare the variables in the html file

<script">
var width = 330
var height = 306
</script>

And then call the javascript that uses the file which contains...

<script language="JavaScript">
document.write("Height=" + height + " Width=" + width);
</script>

Hopefully that made sense. :)

Wes

scriptmasterdel

6:35 pm on Feb 21, 2007 (gmt 0)

10+ Year Member



It's pretty simpe to declare variables in javascript and then use them in an external Javascript file.

What you need to do is firstly declare te variables like you have before.

<script>
var myWidth = 330
var myHeight = 306
</script>

And then include the external JS file as follows:

<script src="external-file.js"></script>

And then you can use the variables in the "external-file.js" like so:

alert("Height=" + myHeight + " Width=" + myWidth);

I hope this helps.