473,394 Members | 1,517 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,394 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 3188
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

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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: 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 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.