Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / October 2006



Tip: Looking for answers? Try searching our database.

What is the scope of Javascript variables?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Trev - 29 Oct 2006 12:08 GMT
Hi all,
Are variables in javascript local to the page, or to the <script> tags
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>
Laurent Bugnion - 29 Oct 2006 13:20 GMT
Hi,

> Hi all,
> Are variables in javascript local to the page, or to the <script> tags
[quoted text clipped - 22 lines]
>
> </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
Signature

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

Trev - 30 Oct 2006 18:51 GMT
Thanks, my friend. I am perplexed though because I have done the
following:

<HEAD>

<script>

var myBoolean=new Boolean(true);

// Do other processing, alter value of myBoolean accordingly

</script>

</HEAD>

<BODY>

<script>

if (myBoolean) //**************
{
// Do other stuff
}

</script>

</BODY>

My browser (Netscape 6) displays an error, saying that "Error:
myBoolean is not defined" at the line marked above with the asterisks.
Whats gone wrong? It all looks fine to me....

TIA

Trev
Trev - 30 Oct 2006 19:02 GMT
I don't know if this is relevant, but the if statement in the second
script tag is encased within a function:

<BODY>

<script>

function DoMyStuff(){

if (myBoolean) //**************
{
// Do other stuff
}

}
</script>

</BODY>
Randy Webb - 30 Oct 2006 19:04 GMT
Trev said the following on 10/30/2006 12:51 PM:
> Thanks, my friend. I am perplexed though because I have done the
> following:

<snip>

> <script>
>
> var myBoolean=new Boolean(true);

var myBoolean = true;

No need for the new Boolean.

> // Do other processing, alter value of myBoolean accordingly

<snip>

> if (myBoolean) //**************
> {
> // Do other stuff
> }

<snip>

> My browser (Netscape 6) displays an error, saying that "Error:
> myBoolean is not defined" at the line marked above with the asterisks.
> Whats gone wrong? It all looks fine to me....

NS6? NS is up to version 8 and 6 is considerably old. It may be a bug in
NS6 that didn't handle new Boolean() <shrug>. Try changing it to just =
true, test it and see what happens. If it still throws the error, insert
alerts to debug it to see where it "disappears" at. And then look into
upgrading your NS :) Firefox is free and NS6/7 are based on Firefox (to
an extent anyway), and NS8 uses two rendering engines - IE and Mozilla.

Signature

Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Michael Winter - 31 Oct 2006 02:51 GMT
[snip]

> var myBoolean=new Boolean(true);

[snip]

> if (myBoolean) //**************

This is unlikely to achieve what you expect. The variable, myBoolean,
will reference an object. References always evaluate to true when
type-converted to boolean so even:

  new Boolean(false)

would pass that test. If you were to insist on using a Boolean object,
you would have to make use of the valueOf method:

  if (myBoolean.valueOf())

to obtain the boolean value represented by the object. However, as Randy
pointed out, a boolean value will do.

[snip]

Mike
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.