Link to home
Start Free TrialLog in
Avatar of Sonny9
Sonny9

asked on

global variables in javascript

here is my main.js

var m_Xcoord

function startup(){

m_XCoord = document.getElementById("txtXCoord");

}

function calc(){

return m_Xcoord * 4

}


here is default.aspx

<head>
  <SCRIPT language="JavaScript" src="javascript/main.js" type="text/javascript"></SCRIPT>
</head>

<BODY style="MARGIN: 0px" onload="startUp()" MS_POSITIONING="GridLayout">


here is my support.axpx

<head>
  <SCRIPT language="JavaScript" src="javascript/main.js" type="text/javascript"></SCRIPT>
</head>


i open both pages in a frameset

when I call calc() from default.aspx it works fine

but then when I call calc() from support.aspx, the m_Xcoord is null

my question:
what is the life or scope of m_Xcoord
why is it null when I call the calc() from support.aspx
Avatar of 0h4crying0utloud
0h4crying0utloud



What kind of object is "txtXCoord". It seems odd that you are multiplying by 4. Do you mean to get the value of the txtXCoord element?

Is this what you meant to do for startup()?

function startup(){
  m_XCoord = document.getElementById("txtXCoord").value;
}
Avatar of Sonny9

ASKER

txtXCoord is a textbox. both functions work fine I may be missing some details in the code.  But the code is just for example my question is regarding the variable.

I see, I couldn't think of why a global var would not have page scope unless something is interfereing
Avatar of Sonny9

ASKER

page scope?

when you say "page scope", does this mean if I call a function in main.js from support.aspx it won't have the scope?
Hi Sonny,

Yep, I think that is so.

Just noticed that here you've spelled m_XCoord two ways (note caps):

m_XCoord
and
m_Xcoord

That would be a problem.

Peace and joy.  mvan
Another thing. If m_Xcoord is a number and not a reference to your input, then your startUp function should be changed to:

function startup(){

m_Xcoord = document.getElementById("txtXCoord").value;

}

i.e. refering to the value of the input and not the input object.
js file variables gets initialized for each of the page in which they are referensed..

as per ur code :
U r assigning value to m_Xcoord  in the onLoad event of default.aspx only not in the other one ( support.axpx)

Does txtXCoord textbox exist in the both pages ???

just remember scope is page... thats why m_Xcoord  is null when calc is called from support.aspx
Avatar of Sonny9

ASKER

To garima,

so the scope of global variables on a js file is page specific?  By this, I mean if the file is called from the same page the variables hold their values and as soon as I call the js file from different page the variables will be null?
Hi Sonny,

Here is a must read for you:  "Accessing Global Variables of Other Pages"

http://www.infimum.dk/HTML/JSwindows.html#ref_3_12

It starts out with "Each page has its own Javascript environment with its own global variables. These are properties of the window object."  It then goes on to explain how you CAN access JavaScript variable from another frame.  :-)

If you have two frames, named "left" and "right", then to have the page in the "left" frame access a global variable in the "right" frame, the procedure is:
myLeftVariableName = parent.frames["right"].rightGlobalVariableName

Of course, this is not easily made implicit in order to generalize the code.  That is, there's not an easy way to create a single .js file, utilitzing a single set of functions, which can be loaded into frames with arbitrary names and have them access each other's global variables.  But at least this shows you that it can be done, and how to do it.  :-)

Peace and joy.  mvan
ASKER CERTIFIED SOLUTION
Avatar of mvan01
mvan01
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
P.S.  It could be coded without the frame name, if you depend on the defining frame being the first frame in the frameset, then it is called:
parent.frames[0]

and it's variables are like this:
parent.frames[0].abc

to access:
var abc=33;

for example.  The frames are numbered sequentially, starting from 0.

Peace and joy.  mvan