469,356 Members | 2,666 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

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 3047
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

Post your reply

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

Similar topics

8 posts views Thread by David Hitillambeau | last post: by
4 posts views Thread by Andrew V. Romero | last post: by
4 posts views Thread by Dan Elliott | last post: by
2 posts views Thread by Thomas Matthews | last post: by
10 posts views Thread by Charles O'Flynn | last post: by
9 posts views Thread by Ed Jensen | last post: by
112 posts views Thread by istillshine | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.