473,704 Members | 2,347 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

use of a local variable

3 New Member
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> 
  8. <script type="text/javascript"> 
  9. function some_Other_Function() { 
  10.   somecode....;
  11.  } 
  12. </script> 
  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

Oct 10 '08 #1
2 1410
rnd me
427 Recognized Expert Contributor
how you have it will work as long as some_Function is called before some_Other_Func tion.

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
3 New Member
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

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 print "x:", x f()
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 addition & subtraction, pointers can be modified, both are similar. It stated me going what are the diferrence between a reference vairable and local variable ? Here are what I can think of 1. For a local variable constructor is called, not the case...
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;
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 init to 0, like this colorIndex = 0 and in one of my functions, I increment it by 1
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 the contents of the malloc object in main driver module is copied. Which would mean that if I free the object_copy it should not effect
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 that the variable could be deallocated from memory and the dereferencing of the returned pointer somewhere else could crash the program? If it's not safer to do such an operation, how do functions like strtok for example work??? --
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 C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
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; /* WARNING here */ for(index = 0; index < MAX; index++) { ... With '-Wshadow', this innocent code spits the warning:
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; } //---------------------------------------------------------------------------
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 this case, Python doesn't find a "value" variable in this scope, so it checks the outer scope, and does find it. But then when it gets to the "a = " part... well, I don't know, but it doesn't like it.)
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.