JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 11th, 2006, 10:51 AM
pbd22 pbd22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 9 pbd22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 56 sec
Reputation Power: 0
Exclamation Altering the Global Variable??

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.

Reply With Quote
  #2  
Old March 11th, 2006, 02:08 PM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 140 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 33 m 14 sec
Reputation Power: 13
Have you tried the code you posted?

Reply With Quote
  #3  
Old March 11th, 2006, 11:59 PM
pbd22 pbd22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 9 pbd22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 56 sec
Reputation Power: 0
Red face

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.

Reply With Quote
  #4  
Old March 12th, 2006, 12:40 AM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 140 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 33 m 14 sec
Reputation Power: 13
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".

Reply With Quote
  #5  
Old March 12th, 2006, 11:43 AM
pbd22 pbd22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 9 pbd22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 56 sec
Reputation Power: 0
Red face

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.

Reply With Quote
  #6  
Old March 12th, 2006, 01:20 PM
pbd22 pbd22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 9 pbd22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 56 sec
Reputation Power: 0
Arrow

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>

Reply With Quote
  #7  
Old March 12th, 2006, 06:28 PM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 140 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 33 m 14 sec
Reputation Power: 13
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.

Reply With Quote
  #8  
Old March 12th, 2006, 08:19 PM
pbd22 pbd22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 9 pbd22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 56 sec
Reputation Power: 0
Red face thanks for the reply

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.

Reply With Quote
  #9  
Old March 13th, 2006, 12:04 AM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 140 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 33 m 14 sec
Reputation Power: 13
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>

Reply With Quote
  #10  
Old June 4th, 2010, 05:44 PM
ambercf ambercf is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2010
Posts: 1 ambercf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 2 sec
Reputation Power: 0
Use Hidden Input Control

To achieve this, you could set the value property of a hidden input control in the first function, then retrieve that value from the second function.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Altering the Global Variable??


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap