Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable scope 1

Status
Not open for further replies.

duffs

Programmer
Sep 22, 2000
48
US
I don't understand why the below example doesn't work. I thought variables are global unless they are in a function:

test.html contains
<!doctype html public &quot;-//w3c//dtd html 4.0 transitional//en&quot;>
<HTML>
<BODY>
<script src=&quot;c:/tempcandelete/menutop.js&quot;></script>
</BODY>
</HTML>

menutop.js contains
document.write('<scr'+'ipt language=javascript src=&quot;c:/tempcandelete/test2.js&quot;><\/scr'+'ipt>');

if (blah == &quot;blah&quot;)
{
alert (&quot;blah&quot;)
}
else {
alert (&quot;not blah&quot;)
};

test2.js contains
var blah = &quot;bldah&quot;;
alert (&quot;in test2&quot;);

When I try to render test.html, I get:
Error: blah is undefined
and then I get the alert &quot;in test2&quot;

Any way I can get this to work?
TIA
 
test2.js is not included until the page loads, since you're using write()



=========================================================
while (!succeed) try();
-jeff
 
So, is there any trick I can do so that this will work?
 
don't let your script in menutop.js that depends on test2.js fire until the page loads...

window.onload = function() {
if (blah == &quot;blah&quot;)
{
alert (&quot;blah&quot;)
}
else {
alert (&quot;not blah&quot;)
};
}

=========================================================
while (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top