473,396 Members | 2,059 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,396 software developers and data experts.

declare globals in .JS file

If I declared an array or object in a .JS file, should it not be global
throughout all the files which reference it?
Jul 20 '05 #1
4 4742

"Bill" <no@where.com> schreef in bericht
news:bm**********@titan.btinternet.com...
If I declared an array or object in a .JS file, should it not be global
throughout all the files which reference it?


Hi Bill.

Short answer: No.

Long version: A .js file is no more and no less than an include.

<script type=text/javascript src=a.js><script>
(a.js being: var a=1)

--and--

<script type=text/javascript>var a=1<script>

are identical.

If you want a global var or object between pages, use cookies, frames or a
db serverside.

Fred

Jul 20 '05 #2
Lee
Bill said:

If I declared an array or object in a .JS file, should it not be global
throughout all the files which reference it?


No.
The .js file is just a text file. It doesn't provide any place to
store the data.
As each file loads and interprets the file, each one creates its
own storage space.

Jul 20 '05 #3
Lee <RE**************@cox.net> wrote in message news:<bm*********@drn.newsguy.com>...
Bill said:

If I declared an array or object in a .JS file, should it not be global
throughout all the files which reference it?


No.
The .js file is just a text file. It doesn't provide any place to
store the data.
As each file loads and interprets the file, each one creates its
own storage space.


I think it's better to think in terms of windows, not files.
Variables are attached to window objects. Loading a window
or frame creates a new window object. The old window
object is gone, along with its variables. In one window,
you can load several .js files and they can refer to one
common set of variables.

Hence a common technique for "global" variables is to use
frames and define the variables in the frameset or frame
that never goes away. Any frame can access the frameset
variables using top.variablename.
Jul 20 '05 #4
Lee
Chris Riesbeck said:

Lee <RE**************@cox.net> wrote in message
news:<bm*********@drn.newsguy.com>...
Bill said:
>
>If I declared an array or object in a .JS file, should it not be global
>throughout all the files which reference it?


No.
The .js file is just a text file. It doesn't provide any place to
store the data.
As each file loads and interprets the file, each one creates its
own storage space.


I think it's better to think in terms of windows, not files.


Yes. That was actually a blunder on my part.
I had meant to say "As each *window* loads ...".

Jul 20 '05 #5

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:
2
by: tedsuzman | last post by:
----- def f(): ret = 2 exec "ret += 10" return ret print f() ----- The above prints '12', as expected. However,
45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
6
by: Paddy | last post by:
Hi, I got tripped up on the way eval works with respect to modules and so wrote a test. It seems that a function carries around knowledge of the globals() present when it was defined. (The...
2
by: www.douglassdavis.com | last post by:
For example, if I put this code in a file called globaltest.php: $new_var="This is a string"; class TestClass { function __construct() { global $new_var;
13
by: MJ | last post by:
as topic, if i wan to create an array where the content of the array can be edited by form1 and form2, how i going to do it? for example the content of array is {1,2,3} form2 change the content...
16
by: didier.doussaud | last post by:
I have a stange side effect in my project : in my project I need to write "gobal" to use global symbol : .... import math .... def f() : global math # necessary ?????? else next line...
15
by: esha | last post by:
I need to have a Public variable in my project. In VB it can be declared in a standard module. Where can I do it in C# ? I tried to do it in default class Program.cs and I tried it in an added by...
1
by: cokofreedom | last post by:
if __name__ == '__main__': print "Globals (For Loop):" try: for i in globals(): print "\t%s" % i except RuntimeError: print "Only some globals() printed\n" else: print "All globals()...
3
by: r0g | last post by:
Hi There, I'm refactoring some old code that uses global variables and was originally written in one big flat file with a view to nicening it up and then extending it. The problem I have though...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.