By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Global variable

P: n/a
Hi,

If I have a global variable in one <SCRIPT>...</script>, is it
available in the 2nd <SCRIPT>...</script>. I do not know if "global"
means the whole document or the whole <SCRIPT>...</script> only.

Chris
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Lee
chirs said:

Hi,

If I have a global variable in one <SCRIPT>...</script>, is it
available in the 2nd <SCRIPT>...</script>. I do not know if "global"
means the whole document or the whole <SCRIPT>...</script> only.

<html>
<body>

<script type="text/javascript">
alpha="Hello, world!";
</script>

<script type="text/javascript">
alert(alpha);
</script>

It's often easier to test than to ask.

</body>
</html>

Jul 20 '05 #2

P: n/a
> If I have a global variable in one <SCRIPT>...</script>, is it
available in the 2nd <SCRIPT>...</script>. I do not know if "global"
means the whole document or the whole <SCRIPT>...</script> only.


First of all, chirs (if that's your real name), you should get in the habit of
getting your capitalization right.

<SCRIPT>...</script>

is at best sloppy, and sometimes terrible wrong. You usually want to use all
lower case.

In browsers, the so-called window object is the global object. All of the
scripts of a document will share the same window object.

http://www.crockford.com/javascript/survey.html

Jul 20 '05 #3

P: n/a
"global" variables are really members of the default object (window).
So, outside of a function, the following 3 statements are equivalent:
x=1
var x=1
window.x=1

and inside of a function, the following 2 statements are equivalent:
x=1
window.x=1

You can initialize and access those "global" variables from separate script
blocks, wherever window is available in fact.

Hope this helps
"chirs" <ym*@kicon.com> a écrit dans le message de
news:4c**************************@posting.google.c om...
Hi,

If I have a global variable in one <SCRIPT>...</script>, is it
available in the 2nd <SCRIPT>...</script>. I do not know if "global"
means the whole document or the whole <SCRIPT>...</script> only.

Chris

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.