Valhalla
05-27-2004, 05:46 PM
First, I should start by saying I'm new to JavaScript, but I know Java pretty well, so picking up JS hasn't been too bad. There is one thing I am fairly perplexed about, however.
I am working on a site redesign and we are using a mix of CSS and JavaScript. There are 5 separate JavaScript files in our site:
template.js
header.js
footer.js
menu.js
footernav.js
Currently, things work (this site was designed by another student), but I have found that there are many redundancies and inefficiencies with the way the files interact, so I'm trying to restructure it.
Enough background for now - here's my problem:
It is my understanding that when you write:
var variablename = "";
at the top of a .js document, that variable is a global variable and should be visible to all functions within that file. However, I am finding that some functions within the files can't see modifications to the variable. I'll give an example:
in the file header.js, I have the following code
var menuList="";
function drawHeader() {
document.write("<script type='text/javascript' src='http://www.uwsa.edu/" + directory + "menu.js'></script>");
...(some more code)....
}
The menu.js file then repeatedly calls a function from the header.js file (the one I just showed the code from). The purpose of these calls is to continually update a string and later print it out. Here is the code from that function:
function menuEntry(menuTitle, menuAddress, page)
{
menuList=menuList+"<a href='"+menuAddress+"'>"+menuTitle+"</a>";
}
However, what I have discovered is that after the menu.js file has completed executing, menuList does not hold onto the value it had while menuList was executing. I have verified that the fuction was working correctly by putting "document.write" statements into the menuEntry function shown above.
However, when I put a document.write statement on the menuList variable immediately after the <script> tag, the value has been lost and it prints nothing.
I'm sorry if this is unclear - I can send/post the entire files if needed. Thanks for any help with this
I am working on a site redesign and we are using a mix of CSS and JavaScript. There are 5 separate JavaScript files in our site:
template.js
header.js
footer.js
menu.js
footernav.js
Currently, things work (this site was designed by another student), but I have found that there are many redundancies and inefficiencies with the way the files interact, so I'm trying to restructure it.
Enough background for now - here's my problem:
It is my understanding that when you write:
var variablename = "";
at the top of a .js document, that variable is a global variable and should be visible to all functions within that file. However, I am finding that some functions within the files can't see modifications to the variable. I'll give an example:
in the file header.js, I have the following code
var menuList="";
function drawHeader() {
document.write("<script type='text/javascript' src='http://www.uwsa.edu/" + directory + "menu.js'></script>");
...(some more code)....
}
The menu.js file then repeatedly calls a function from the header.js file (the one I just showed the code from). The purpose of these calls is to continually update a string and later print it out. Here is the code from that function:
function menuEntry(menuTitle, menuAddress, page)
{
menuList=menuList+"<a href='"+menuAddress+"'>"+menuTitle+"</a>";
}
However, what I have discovered is that after the menu.js file has completed executing, menuList does not hold onto the value it had while menuList was executing. I have verified that the fuction was working correctly by putting "document.write" statements into the menuEntry function shown above.
However, when I put a document.write statement on the menuList variable immediately after the <script> tag, the value has been lost and it prints nothing.
I'm sorry if this is unclear - I can send/post the entire files if needed. Thanks for any help with this