Google Answers Logo
View Question
 
Q: Javascript: passing variables between scripts on the same page. ( Answered 5 out of 5 stars,   0 Comments )
Question  
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

Request for Question Clarification by secret901-ga on 02 Mar 2003 11:17 PST
Hi mhffs,
Are the scripts declared in the body of your page?  If you declare all
your functions in the head of your page, then they should be able to
pass variables around each other.

secret901-ga

Clarification of Question by mhffs-ga on 02 Mar 2003 11:31 PST
Thanks,
Let me check it out and see if it works.

I'll get back to you in a minute.

Martin

Clarification of Question by mhffs-ga on 02 Mar 2003 11:41 PST
It doesn't seem to work.  Let me clarify.

I have a button that sends the user to a url:
www.someweb.com?variable= X
X is a product of a script that runs after the page has loaded and
changes depending on what the user selections are.

function calc(){
var x = (variable1 / variable2);
}
function userInput1(){
 variable1 = adb;
}
function userInput2(){
 variable2 = arnd;
}

What isn 't working is passing "variable1" from "userInput1" to
"calc()"

HELP!!!
Answer  
Subject: Re: Javascript: passing variables between scripts on the same page.
Answered By: jdog-ga on 02 Mar 2003 11:59 PST
Rated:5 out of 5 stars
 
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:5 out of 5 stars
Worked Like a Charm....It is SO NICE to have knowledgable help with these items.
Thank you Google Answers!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

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 Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy