• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6308
  • Last Modified:

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
0
Sonny9
Asked:
Sonny9
  • 4
  • 3
  • 3
  • +2
1 Solution
 
0h4crying0utloudCommented:


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?
0
 
0h4crying0utloudCommented:

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

function startup(){
  m_XCoord = document.getElementById("txtXCoord").value;
}
0
 
Sonny9Author Commented:
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.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
0h4crying0utloudCommented:

I see, I couldn't think of why a global var would not have page scope unless something is interfereing
0
 
Sonny9Author Commented:
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?
0
 
mvan01Commented:
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
0
 
BatalfCommented:
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.
0
 
garima_sikarwarCommented:
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
0
 
Sonny9Author Commented:
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?
0
 
mvan01Commented:
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
0
 
mvan01Commented:
Hi Sonny,

Try these files.  :-)  The same calc() function is called from both frames.  The hard coded detail is that the name of the defining frame is "upper".  You can see this in main.js

Peace and joy.  mvan

----- main.js -----
var m_XCoord;

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

function calc(){
   if (self.frames.name != 'upper') {
      return parent.frames['upper'].m_XCoord * 4;
   } else {
      return m_XCoord * 4;
   }
}

----- defalut.htm -----
<html>
<head>
<title>mvan - Q_21870468 default.htm</title>

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

</head>
<BODY style="MARGIN: 0px" onload="startup()">
<input type=textbox name="txtXCoord" id="txtXCoord" value="17"><br>
<input type=button value="calc Test" onclick="alert('default.htm says: calc() = '+calc())">
</BODY>
</html>

----- support.htm -----
<html>
<head>
<title>mvan - Q_21870468 support.htm</title>

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

</head>
<BODY>
<input type=button value="calc Test" onclick="alert('support.htm says: calc() = '+calc())">
</BODY>
</html>

----- framer.htm -----
<html>
<head>
<title>mvan - Q_21870468 test</title>
</head>
<frameset rows="50%,*">
<frame name="upper" src="default.htm">
<frame name="lower" src="support.htm">

<BODY>
<noframes>This test loads two files into frames</noframes>
</BODY>
</html>
0
 
mvan01Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now