473,288 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

use of a local variable

3
I have a variable with a numberic value calculated/defined within a function. In some other part of the page, I want to be able to use the value of that variable; however, since that variable is generated as part of the previous function, it is defined as a local variable and its value is not available to the other function.

Is there a way to be able to transport the value of a local variable to a function that is out of scope? I welcome any suggestions.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript"> 
  2. function some_Function() { 
  3.   somecode....;
  4.   var_Local = some_Value;
  5.  } 
  6. </script> 
  7.  
  8. <script type="text/javascript"> 
  9. function some_Other_Function() { 
  10.   somecode....;
  11.  } 
  12. </script> 
  13.  
  14. <form action="http://www.mydomain.com/results.php" method="post" 
  15. name="form" onsubmit="return (some_Other_Function(var_Local))"> 
  16. </form> 
As I described above, I would like to be able to use the value of the var_Local as part of the onsubmit, but I am getting a var_Local is unknown error, even though I can see/write its value if I do document.write within the some_Funciton() function

Thanks
Oct 10 '08 #1
2 1399
rnd me
427 Expert 256MB
how you have it will work as long as some_Function is called before some_Other_Function.

when you don't use the var keyword, you create not a local, but a global variable.
var_Local = some_Value; //makes global
var Local = some_Value; //makes local

since the value of the variable is set inside of some_Function, it must be called in order for the variable to be set as expected.


there is a third category, protected.
these are like globals except they live inside of a parent function that encloses both of the other functions.

a fourth option is an object property.
i recommend using these instead of globals whenever possible.
you can simply tack the variable onto the function object it lives in:

Expand|Select|Wrap|Line Numbers
  1. function some_Function() { 
  2.   somecode....;
  3.   var Local = "some_Value";
  4.   some_Function.Local = Local;
  5.  } 
you can then access the variable from any function like this:

Expand|Select|Wrap|Line Numbers
  1. function some_Other_Function() { 
  2.  alert(some_Function.Local);
  3.  } 
Oct 10 '08 #2
tolgs
3
4th option was the one. thank you very much for your time!!
Oct 17 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
2
by: Kench | last post by:
I was curious and playing with pointers and references to see what's different between them. Other than the obvious ones involving C++ syntax & things like references cannot be modified with...
9
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
2
by: silverburgh.meryl | last post by:
Can you please tell me what is the meaning this error in general? UnboundLocalError: local variable 'colorIndex' referenced before assignment In my python script, I have a variable define and...
5
by: FireHead | last post by:
Hello All, Its hard to explain but here it goes: char &free_malloc(char* object_copy){ Here ------------------------------------------------------------------ object_copy is parametre were...
14
by: Spitfire | last post by:
Hi All, I've this weird question about pointers. I would like to know how to return the address of a local variable, safely!! Isn't that a unrecommended procedure? Doesn't it have possibilities...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
25
by: guisep4 | last post by:
I have unusual "problem" with naming local variable "index". It all began when I added -Wshadow to gcc commandline. The code is like this: #include <string.h> int foo(void) { int index; ...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
2
by: Matt Nordhoff | last post by:
Sebastjan Trepca wrote: Python doesn't like when you read a variable that exists in an outer scope, then try to assign to it in this scope. (When you do "a = b", "b" is processed first. In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.