![]() |
|
,
0 Comments
)
|
| Subject:
Javascript: passing variables between scripts on the same page.
Category: Computers > Programming Asked by: mhffs-ga List Price: $5.00 |
Posted:
02 Mar 2003 10:55 PST
Expires: 01 Apr 2003 10:55 PST Question ID: 169592 |
Hi, I have a page with several javascripts in it. For some reason I can't seem to pass variables between the scripts and I need to for one of the functions. I understand this can be done by giving each script a name and referencing the script name with the variable. Could you provide an example of this, or another method of passing variables between scripts. Thanks, Martin | |
| |
| |
|
|
| Subject:
Re: Javascript: passing variables between scripts on the same page.
Answered By: jdog-ga on 02 Mar 2003 11:59 PST Rated: ![]() |
Hello mhffs,
I believe I see your problem: like some other languages, JavaScript
has a notion of 'scope' (although quite a quite limited one in
comparison). If you declare a variable inside of one function is JS,
you can't use it in another function. The simple work around is to
declare all variables 'globally' (often considered bad style in other
languages, but pretty common in JS). Alternatively, you could make the
two helper functions return values and call them from calc, but the
other way is probably more straight forward for you. Make the
following simple change and your script should work:
<SCRIPT Type="text/javascript" Language="JavaScript">
<!--
var variable1; // declare the variables outside of the functions
var variable2;
function calc(){
var x = (variable1 / variable2);
}
function userInput1(){
variable1 = adb;
}
function userInput2(){
variable2 = arnd;
}
//-->
</SCRIPT>
Please don't hesitate to ask for a clarification if you need more
help.
JDog-ga |
mhffs-ga
rated this answer:
Worked Like a Charm....It is SO NICE to have knowledgable help with these items. Thank you Google Answers! |
|
| There are no comments at this time. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |