n javascript
i m nt able to access global variables defined in a separate .js file and accessed by different pages
eg.one page has got two frame
when one frame changes the value of a global variable thn othr frame is nt able to read it...
3 3189
Here's some vowels for you in case you are running low:
aaaaaaaaaaaaaeeeeeeeeeeeeeeiiiiiiiiiiiiiiiiiiioooo oooooooooouuuuuuuuuuuuu
When you declare a variable either outside of a function or without using the var keyword, it becomes a property of the window object, and any script can access it (since 'with window' is assumed).
In other words: -
<script type="text/javascript">
-
var g_One = 'I am global because my context is window.';
-
g_Two = 'I am also global.';
-
window.g_Three = 'Also global.';
-
window['g_Four'] = 'Yup. Global.';
-
this.g_Five = 'Probably global, but not reliable, since {this} might not be window.';
-
-
function doSomething() {
-
var notGlobal = 'I am *not* global because my context is doSomething, not window.';
-
-
g_Six = 'This is global, because {with window} is assumed.';
-
window.g_Seven = 'Global!';
-
-
this.g_Eight = 'Probably global, but not reliable, since {this} might not be window; it depends on how doSomething gets used.';
-
}
-
</script>
-
Anything defined within an iframe should still be accessible, but window might not be an assumed context. It's been years since I've programmed for iFrames, but if you're sure it's a global, try this: -
// If this line doesn't work...
-
alert(myVar);
-
// ... try this instead.
-
alert(window.myVar);
-
// Or:
-
alert(window['myVar']);
-
-
// If that doesn't work, check where you define myVar and make sure it's getting attached to the window.
-
gits 5,390
Expert Mod 4TB
good explaination ... and using globals often drives you to the 'globals hell' :) ... and only some ideas to add, some that you should have in mind in the context of the above problem:
trying to avoid globals, is mostly a good recommendation ... as your javascript-code grows ... and you use more and more components(classes, methods etc.) you may want to know where your vars are set or where they are used ... so it is good practice to pass vars from one obj to an other ... so if you need to debug your code you are able to backtrace through the chain of method-calls ... that's only my point of view to that ... :)
if you use frames and you want to use globals, and you agree a little bit with the above statement ... it would be a good practice to pass a reference to the global var you want to use to every frame ... so that you are able to refer to it in its own execution context ... therefore you should have a global object (window would be that too ... don't pass a primitive var to it, they are passed by value and you need the reference, until the var could be set from everywhere and you want the actual value) ... and you pass a ref to it to the frame where you now be able to refer to that reference (avoids parent, parent.parent references that have to be adapted every time to change your layout etc. -> with the ref you only have to adapt the ref where it is passed) ... but be aware: wheter you pass it that way or not ... you have to assure that your global scope is initialized at first! ... that will say: assuming you are loading 2 pages into frames ... you want to use global vars that are initialized and/or used in onload of one page or the framset ... you have to wait for that initialization before using the desired vars ... noone of the 3 pages know, when the other is really 'loaded' ... so you have to assure it yourself ... the onload of every page may register a value or you may ask the frames/window for a property that assures, the document is loaded - all src-loads in html are async ... you know it ... i know :)
ok
What i am currently using is defining all the global variables in the index page
and accessing them using parent.[var_name] from all the frames.
It's working fine but not too sure about good practices
hope i have not missed any vowel :D
shoonya
Sign in to post your reply or Sign up for a free account.
Similar topics
by: David Hitillambeau |
last post by:
Hi guys,
As I am new to Python, i was wondering how to declare and use global
variables. Suppose i have the following structure in the same module (same
file):
def foo:
<instructions>...
|
by: Andrew V. Romero |
last post by:
I have been working on a function which makes it easier for me to pull
variables from the URL. So far I have:
<script language="JavaScript">
var variablesInUrl;
var vArray = new Array();
...
|
by: Dan Elliott |
last post by:
Hello,
Converting from a working C program to C++, I run into the following error:
I have a header: (header.h)
namespace shared{
...
struct X{
...
|
by: Thomas Matthews |
last post by:
Hi,
I'm getting linking errors when I declare a variable in the
global scope, but not inside a function. The declarations
are the same (only the names have been changed...).
class Book
{...
|
by: lawrence |
last post by:
I'm learning Javascript. I downloaded a script for study. Please tell
me how the variable "loop" can have scope in the first function when
it is altered in the second function? It is not defined...
|
by: MLH |
last post by:
A97 Topic: If there is a way to preserve the values assigned to
global variables when an untrapped runtime error occurs? I don't
think there is, but I thought I'd ask.
During development, I'm...
|
by: Charles O'Flynn |
last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in
general, I have 'dived in' to start a small project to read and parse an XML
data stream. I have already worked out most of...
|
by: Ed Jensen |
last post by:
I'm having a vexing problem with global variables in Python. Please
consider the following Python code:
#! /usr/bin/env python
def tiny():
bar =
for tmp in foo:
bar.append(tmp)
foo = bar
|
by: weaknessforcats |
last post by:
C++: The Case Against Global Variables
Summary
This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
|
by: istillshine |
last post by:
When I control if I print messages, I usually use a global variable
"int silent". When I set "-silent" flag in my command line
parameters, I set silent = 1 in my main.c.
I have many functions...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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...
| |