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

Where can I declare Global Variables

P: n/a
Noob problem. I prefer to keep all my scripts in an external '.js' file.
I am currently loading the external '.js' file from the header. Problem is I
would like to declare
a global variable in the external file, but I keep getting an error about
the object does not exist.

Can someone tell me where or how to declare a global variable in an external
file that is
available after the page is loaded.

Thanks in advance.
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
rick wrote:
Noob problem. I prefer to keep all my scripts in an external '.js' file.
I am currently loading the external '.js' file from the header. Problem is I
would like to declare
a global variable in the external file, but I keep getting an error about
the object does not exist.

Can someone tell me where or how to declare a global variable in an external
file that is
available after the page is loaded.

Thanks in advance.


- Variables declared within a function are local to that function
(accessible only within that function)

http://lists.evolt.org/archive/Week-...03/136552.html

.... variables declared withing a function are defined only within the
body of the function. They are local variables and have local scope.
Function parameters also count as local variables and are defined only
within the body of the function.

- Variables declared outside a function are global
(accessible from anywhere on the page)

If you keep getting an error post your code or a url where it can be seen.
Mike

Jul 23 '05 #2

P: n/a
I found the answer: Do not use 'var' to declare the variable.
Just declare it!! i.e. 'newvar = 0;'

"mscir" <ms***@access4less.com.net.org.uk> wrote in message
news:10*************@corp.supernews.com...
rick wrote:
Noob problem. I prefer to keep all my scripts in an external '.js' file.
I am currently loading the external '.js' file from the header. Problem is I would like to declare
a global variable in the external file, but I keep getting an error about the object does not exist.

Can someone tell me where or how to declare a global variable in an external file that is
available after the page is loaded.

Thanks in advance.


- Variables declared within a function are local to that function
(accessible only within that function)

http://lists.evolt.org/archive/Week-...03/136552.html

... variables declared withing a function are defined only within the
body of the function. They are local variables and have local scope.
Function parameters also count as local variables and are defined only
within the body of the function.

- Variables declared outside a function are global
(accessible from anywhere on the page)

If you keep getting an error post your code or a url where it can be seen.
Mike

Jul 23 '05 #3

P: n/a
"rick" <rmsingh@no_spam_rogers.com> writes:

Please don't top post.
I found the answer: Do not use 'var' to declare the variable.
Just declare it!! i.e. 'newvar = 0;'


That shouldn't make a difference, if the declaration is not inside a
function body. Then the declaration will declare the global variable,
just as simply assigning to an undeclared variable.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4

P: n/a
I found the answer: Do not use 'var' to declare the variable.
Just declare it!! i.e. 'newvar = 0;'


That shouldn't make a difference, if the declaration is not inside a
function body. Then the declaration will declare the global variable,
just as simply assigning to an undeclared variable.


That is what I did. Sorry still learning..........
Jul 23 '05 #5

P: n/a
rick wrote:
I found the answer: Do not use 'var' to declare the variable.
Just declare it!! i.e. 'newvar = 0;'


That shouldn't make a difference, if the declaration is not inside a
function body. Then the declaration will declare the global variable,
just as simply assigning to an undeclared variable.


That is what I did. Sorry still learning..........


I think rick is right, declaring a variable without using "var" inside a
function makes the variable global, at least it looks that way to me.
When the button is clicked (see code below) on my IE6, Netscape 7,
Firefox 0.8 the variable 'unknownscopevar' is shown to be initialized.

http://www.southwest.com.au/~jfuller...avascript4.htm

This website makes the claim, 'Generally, variables declared as "var"
are Global variables. Variables declared inside a function as "var" are
local to the function. Variables defined inside a function without the
"var" are Global variables.'

Similar:

http://www.mredkj.com/tutorials/refe..._intro_ex.html

Mike

<script type="text/javascript">
function init() {
unknownscopevar = 'the variable: "unknownscopevar" is initialized';
}
function showvar() {
alert('unkonwnscopevar = '+unknownscopevar);
}
</script>
</head>

<body onload="init()">
<form>
<input type="button" value="Show Variable" onclick="showvar()">
</form>

Jul 23 '05 #6

P: n/a
On Sun, 18 Apr 2004 17:35:53 -0700, mscir
<ms***@access4less.com.net.org.uk> wrote:
rick wrote:
I found the answer: Do not use 'var' to declare the variable.
Just declare it!! i.e. 'newvar = 0;'

That shouldn't make a difference, if the declaration is not inside a
function body. Then the declaration will declare the global variable,
just as simply assigning to an undeclared variable.


That is what I did. Sorry still learning..........


I think rick is right, declaring a variable without using "var" inside a
function makes the variable global, at least it looks that way to me.


He is, and Lasse agreed: "...assigning to an undeclared variable". That
is, a variable without prior use of var. It's also described in Netscape's
JavaScript Guide:

<URL:http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/ident.html#1008330>

[snip]

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.