471,831 Members | 873 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,831 software developers and data experts.

abt global variable in js

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 3069
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.';
  8. function doSomething() {
  9.     var notGlobal = 'I am *not* global because my context is doSomething, not window.';
  11.     g_Six = 'This is global, because {with window} is assumed.';
  12.     window.g_Seven = 'Global!';
  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>
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']);
  8. //    If that doesn't work, check where you define myVar and make sure it's getting attached to the window.
May 11 '07 #2
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
161 100+

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

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
reply views Thread by NeoPa | last post: by
reply views Thread by aboka | last post: by

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.