|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi. I am trying to figure out a way to alter the global variable inside a function and make the new value available to another script. How is this possible?
I have a global variable: var global = "foo"; then i have a function in a script: <script> function local () { global = "bar"; } </script> then i have another script: <script> try{ alert(global); } catch(e){alert("error: " + e.description)} <script> I want the function in the first script to change global from "foo" to "bar" and the alert in the second script should read "bar". Also, local() is used for other purposes and cannot return global. I have a feeling that i am going to be told here that "this is not possible". so, if it isn't possible, what is the work around? thanks in advance. |
|
#2
|
|||
|
|||
|
Have you tried the code you posted?
|
|
#3
|
|||
|
|||
|
the code i posted was set up as an exampe.
but, the result is/would be the initial value of the global variable - "foo"... not the result i am going for which is changing the global variable in the function local() to "bar". so, the code i posted produces "foo" does anybody know how to produce "bar" in the manner i explained? thanks again. |
|
#4
|
|||
|
|||
|
Did you use the "var" keyword inside the function? If you did, remove it and try it again.
Are you sure that you called the function? In the function you can change "global" to "window.global". |
|
#5
|
|||
|
|||
|
hey. thanks.
nope, no "var". and, i tried window.global but that still gives me the original value of global, not the changed value. is this even possible? i seem to gather from web sites that it isnt. I am not "just" trying to change global within a function (I know how to do that), i am trying to change global inside a function within a script and then have that changed value called inside another script. So, for this to happen (i am guessing) the "global" value of "var global = ...", not the "local" value of global, would need to change (I realize now that using "global" as my variable name is kinda confusing,.. sorry). I am trying to change it inside the function local() as explained but am not sure if this is possible. thanks again for your responses. |
|
#6
|
|||
|
|||
|
Hi. Below is the code almost exactly as i have it.
getValues is a callback that takes a bunch of numbers from the server. In that callback, the highest and lowest numbers are assigned to min and max. This is in a master page. A child page is loaded that has a script that wants to use the new min/max values to pass to function render(nums, min, max); Of course, the result is min = 20 and max = 20 from the global declaration, not the changed numbers from getValues_callback. So, if you have any suggestions, much appreciated. tnks. Code:
<script type="text/javascript">
//global values
var min = 20;
var max = 20;
function getValues_callback(response)
{
try {
var dset = response.value;
if(ds!=null && typeof(ds) == "object")
{
for(var i=0; i<=dset.Tables[0].Rows.length-1; i++) {
var info = {s1:ds.Tables[0].Rows[i].first_num,
s2:ds.Tables[0].Rows[i].second_num,
s3:ds.Tables[0].Rows[i].third_num,
s4:ds.Tables[0].Rows[i].fourth_num}
if (info.s1+info.s2) < (min)) { min = "new value 1"; }
if (info.s1+info.s2) > (max)) { max = "new value 2";}
more code that uses the above dataset here...
} // end i loop
} // end if statement
} // end try statement
catch(e){alert("getValues_callback error: " + e.description);} // end catch
} // end getValues_callback function
function render(nums, early, late) {
alert("the earliest time is: "+early+" and the latest time is: "+late);
more code here...
}
</script>
this page is a child page. the above script is on its master page. Here, render() is called that hopes to pass the new min/max values from getValues_callback but doesnt - it still gets the orig global values for min/max. Code:
<script type="text/javascript">
try {
var stuff = {one:23;
two:23;
three:3;
four:45;};
} catch(e){alert(" error: " + e.description);}
render(stuff, min, max);
</script>
|
|
#7
|
|||
|
|||
|
master page and child page... do you mean that you have a popup window?
Global variables are only global within one window. You need to have a reference to the other window stored in a variable to access the variables of that window from a different window. Windows created with window.open() automatically get a reference to the parent with "window.opener". window.open() returns a window reference when it is succesful, so you can store that in a variable for later use -- just don't forget to make that variable global if you want to access it later. |
|
#8
|
|||
|
|||
|
hi.
master/child - i am programming in ASP.NET which uses master pages as a template... child pages are loaded into that template as needed. I am sorry, but i dont really have a good handle on what you are talking about. isn't window.open() used to open a new page in the same browser window? I am not trying to do that. Here is my situation again: * I have 2 scripts on the same page. Script A and Script B. * Script A has a global variable: var global = 'foo'; * Script A also has a function that modifies that variable resulting in a new value: function local() {global='bar';} * Script B further down the same page wants to use the new value of the global variable declared in Script A. The situation is that Script B only reads 'foo' for "global" because the alterned value happened in a local function. The question: How Does Script B access the modified value of Script A's global variable? If I have not understood your post, and what you suggested indeed does exactly this, then i apologise for my thick-headedness and would you mind giving me a code example of what you are saying? I thank you. ![]() |
|
#9
|
|||
|
|||
|
window.open() makes a new browser window. I don't use ASP, so I didn't understand what you meant by "master page" and "child page". You're not using window.open() so what I said in my previous post will not help you resolve this.
Maybe the "try" code blocks are interfering. What happens if you move the code outside them? See in this little demo what I said before works: Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title></title>
<script type="text/javascript"><!--
var global = 'foo';
function local() {
global = 'bar';
}
local();
alert(global)
// -->
</script>
</head>
<body>
</body>
</html>
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Altering the Global Variable?? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|