Hi,
Trev wrote:
Hi all,
Are variables in javascript local to the page, or to the <scripttags
they are defined within?
Say I had a bit of code as follows:
<HEAD>
<script>
var myVar = MyFunction();
</script>
</HEAD>
could I then use myVar as follows?
<BODY>
<script>
MyOtherFunction( myVar );
</script?
</BODY>
Yes, all script content (in internal script tags, or included through
script src) is interpreted in one "application" only.
If you define something (variable, function...) in one place of the
page, you can use it from another place in the same page without doing
anything. This causes problems especially in pages built with
server-side controls, because each control must make sure that each
variable is unique. If two controls define a global variable or a global
function with the same name, the last one to be parsed wins. That's why
global variable or functions are a bad idea as soon as the page gets a
little too complex.
HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering:
http://www.galasoft-LB.ch
PhotoAlbum:
http://www.galasoft-LB.ch/pictures
Support children in Calcutta:
http://www.calcutta-espoir.ch