[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

global variables in javascript

Posted on 2006-05-31
12
Medium Priority
?
6,302 Views
Last Modified: 2012-08-13
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
Comment
Question by:Sonny9
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 10

Expert Comment

by:0h4crying0utloud
ID: 16803544


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
 
LVL 10

Expert Comment

by:0h4crying0utloud
ID: 16803556

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

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

Author Comment

by:Sonny9
ID: 16803570
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:0h4crying0utloud
ID: 16803663

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

Author Comment

by:Sonny9
ID: 16803848
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
 
LVL 11

Expert Comment

by:mvan01
ID: 16804583
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
 
LVL 32

Expert Comment

by:Batalf
ID: 16805787
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
 
LVL 4

Expert Comment

by:garima_sikarwar
ID: 16805944
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
 

Author Comment

by:Sonny9
ID: 16808415
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
 
LVL 11

Expert Comment

by:mvan01
ID: 16811440
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
 
LVL 11

Accepted Solution

by:
mvan01 earned 400 total points
ID: 16811607
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
 
LVL 11

Expert Comment

by:mvan01
ID: 16811653
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question