473,396 Members | 1,827 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.

abt global variable in js

shoonya
161 100+
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...
May 11 '07 #1
3 3189
pbmods
5,821 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var g_One = 'I am global because my context is window.';
  3. g_Two = 'I am also global.';
  4. window.g_Three = 'Also global.';
  5. window['g_Four'] = 'Yup.  Global.';
  6. this.g_Five = 'Probably global, but not reliable, since {this} might not be window.';
  7.  
  8. function doSomething() {
  9.     var notGlobal = 'I am *not* global because my context is doSomething, not window.';
  10.  
  11.     g_Six = 'This is global, because {with window} is assumed.';
  12.     window.g_Seven = 'Global!';
  13.  
  14.     this.g_Eight = 'Probably global, but not reliable, since {this} might not be window; it depends on how doSomething gets used.';
  15. }
  16. </script>
  17.  
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:

Expand|Select|Wrap|Line Numbers
  1. //    If this line doesn't work...
  2. alert(myVar);
  3. //    ... try this instead.
  4. alert(window.myVar);
  5. //    Or:
  6. alert(window['myVar']);
  7.  
  8. //    If that doesn't work, check where you define myVar and make sure it's getting attached to the window.
  9.  
May 11 '07 #2
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 :)
May 11 '07 #3
shoonya
161 100+
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
May 11 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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>...
4
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(); ...
4
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{ ...
2
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 {...
8
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...
17
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...
10
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...
9
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
1
weaknessforcats
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...
112
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...
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
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
Oralloy
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,...
0
jinu1996
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...
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...

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.