473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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 19032
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. How can I do this? example:
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 undeclared variable. I can't beleive this is just for...
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 MODULE_MAIN = HAS_NEVER_BEEN_RECEIVED #endif ;
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 Program
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 variables be placed ? 2. Would i need to forward...
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 declared before the page_Load, so all function can...
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 fine. I wanted to be able to use them in another...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.