472,351 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 software developers and data experts.

Where can I declare Global Variables

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
6 18893
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: NotGiven | last post by:
I have an file I call using: require_once() In this file I have variables I'd like to use in the calling page and functions called by that page....
10
by: Andy Fish | last post by:
Hi, can anybody put forward a sensible argument javascript's behaviour of creating a new global variable whenever I assign to a previously...
4
by: Bill | last post by:
If I declared an array or object in a .JS file, should it not be global throughout all the files which reference it?
6
by: David T. Ashley | last post by:
Hi, In my project, I typically declare and define variables in the .H file, i.e. DECMOD_MAIN UINT8 can_message_201_status_global #ifdef...
5
by: John Salerno | last post by:
Here's my full code: using System; using System.Collections.Generic; using System.Text; using System.IO; namespace PatternMatcher { class...
9
by: vivekian | last post by:
Have a couple of global variables of a class type which i have declared and defined in the application. 1. Not sure where should the global...
9
by: johnlim20088 | last post by:
Hi, Hi, currently I developing an application in C# NET, let say i have a page call viewrecord.aspx, in this page, i have a global variables...
5
by: dancer | last post by:
Using ASP.net 1.1 and VB Is it possible to declare variables in their own subroutine? I had my DIM statements within a sub that worked just...
3
by: sravik | last post by:
how and where to declare global variables in C#. Like Conection strings and some file paths etc.
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.