Solved

retrieving a created variable in function

Posted on 2006-06-15
39
247 Views
Last Modified: 2008-03-17
the following is my function

function getAccountNumber(clientAccountNumber)
{
alert('function called');
alert(clientAccountNumber);      
}

I need to bring clientAccountNumber as a variable out of the function to use within another function
as soon as I take my second alert out of the function I get an error undefined...

what am I doing wrong or what am I missing ?
0
Comment
Question by:prowebinteractive
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 21
  • 13
  • 3
39 Comments
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 16912068
You could use eval ('clientAccountNumber');


0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16912090
The var is localy defined because it is only defined as function parameter.
Change it to this:

function getAccountNumber(acc){
  alert('function called');
  clientAccountNumber = acc; // no var prefix to let it be global
  alert(clientAccountNumber);    
}


0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 16912104
Here is an example

<html>
<head>
<title>Form Field Eval</title>
</head>
<body>
<script language="javascript">
function catchErr(err) {
     alert (err);
}
onerror = catchErr;
clientAccountNumber = 123456;
alert (eval ('clientAccountNumber'));

// try to get undefined variable will show error
alert (eval ('clientAccountNumber2'));
</script>
</body>
</html>

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 63

Expert Comment

by:Zvonko
ID: 16912115
eval() is NOT necessary.
0
 

Author Comment

by:prowebinteractive
ID: 16912135
the following is what I have now and Im still geting undefined...

function getAccountNumber(clientAccountNumber)
{
alert('function called');
myClientAccountNumber = clientAccountNumber;

}
alert(myClientAccountNumber);
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16912164
In that sequence is the function NOT called and therefore no global var defined.
Check this:

function getAccountNumber(clientAccountNumber){
  alert('function called');
  myClientAccountNumber = clientAccountNumber;
}
getAccountNumber(123);
alert(myClientAccountNumber);



0
 

Author Comment

by:prowebinteractive
ID: 16912201
getAccountNumber(123); cant do that, Im working in a external .js file where Im retrieving a PHP variable in the called function
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16912218
You CANNOT retrieve PHP vars in JavaScript.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16912245
What you perhaps miss in your tests is that PHP is executed on web server side and produces a text stream which is flushed and sent to browser.
The browser interpretes that text stream and executes the script statements if it is in <script> context.
But you never can access anything on server side from browser side. Only way is the you server modfies the script source before it is loaded in browser. But then you get the browser script cashing problem and need to change the script file URL at every fetch from server.

0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 16912288
Unless PHP is spitting out the Javascript variable, you can not get it

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16912299
That's the short version of my explanation ;-)
0
 

Author Comment

by:prowebinteractive
ID: 16912362
this is where I call the function:

src='../../include/webMenu.js' language='JavaScript1.2' type='text/javascript'></script><script type='text/javascript'>awmBuildMenu(); getAccountNumber('<? echo $_GET['accountNumber'] ?>');</script>

now as you could see the source of the javascript is in a file called webMenu.js

in the webmenu.js I have the function getAccountNumber();
0
 

Author Comment

by:prowebinteractive
ID: 16912396
well the way I did it Im retrieving the php variable in the js file

this works:
function getAccountNumber(clientAccountNumber)
{
alert('function called');
myClientAccountNumber = clientAccountNumber;
alert(myClientAccountNumber);
}

but this doesnt:

function getAccountNumber(clientAccountNumber)
{
alert('function called');
myClientAccountNumber = clientAccountNumber;

}
alert(myClientAccountNumber);

the variable myClientAccountNumber needs to be used with in the whole .js document so I need to pull myClientAccountNumber out of the function
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16912417
That should work.
But be aware that alert(myClientAccountNumber); will give the passed acc first after the  getAccountNumber('<? echo $_GET['accountNumber'] ?>'); call location on the html page. Before that call is the global var not assigned.

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16912434
Do this:

<script DEFER src='../../include/webMenu.js' language='JavaScript1.2' type='text/javascript'></script><script type='text/javascript'>awmBuildMenu(); getAccountNumber('<? echo $_GET['accountNumber'] ?>');</script>


The DEFER parameter will delay the script source evaluation before the page is completely loaded.

0
 

Author Comment

by:prowebinteractive
ID: 16912535
doesnt work

and it doesnt work only when I pull my javascript variables out of the function....
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16912582
And why do you have to have single steents executed at include file evaluation? That does not make any usefull usage that I could imagine. Put that stand alone statements in a function called at page onLoad event handler time.

0
 

Author Comment

by:prowebinteractive
ID: 16912682
all right I put in onload instead...   it still gave me the error

the error is not where or how I call the function

its taking the value of the declared variable in the function for use outside the function
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16912871
We can go on turning in cycles before you understand basics of script evaluation sequence.
Let us start from the beginning.
This is plain valid:
<script>
doMyFunc(33);
//
function doMyFunc(theNum){
  alert(theNum);
}
</script>

That mean, you can call a function from a script before it occurrence on the page. The reason is that the source is scanned before it is started to be executed.
But this is not valid:
<script>
alert(myVar);
var myVar=33;
</script>


The source is again scanned before execution, but you cannot use the var before it execution assignment statement is executed.
That is your problem in your script.
You have a function in your included source that assigns the global var. That function can be anywhere in that source before or after the single statements that use the global var assigned in that function. The location of the function on the script page does not matter. But what matter is the time or location when it is executed. That mean, the definition of a function alone does not create the global var. The global var is first created after that function is called and executed. Therefore you cannot use nowhere the global var from that script source because the call and creation of that global var is done by call from outside of that script source, namely in the next script. Do you see that problem?

0
 

Author Comment

by:prowebinteractive
ID: 16913019
why do things have to be made so damn complicated
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16913400
:-)

How can we go on solving this? I mean, either you show your online URL and we can analyze what the best approach is or you ask questions. But single lines do not help you out of context.
Does you onLoad approach work now? Can we close this question?

0
 

Author Comment

by:prowebinteractive
ID: 16913475
yes onload works.....

but the fact that I cannot retrieve a variable created in a function, is not working
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16913490
The fact is not true. You can access the var created in a function as soon as the function is executed, but not before it is executed. What is your problem?
0
 

Author Comment

by:prowebinteractive
ID: 16913628
ok makes sence, however I cannot recall the function without an argument remember I have sent a php variable
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16913645
That is a sequence problem, not a script problem.
0
 

Author Comment

by:prowebinteractive
ID: 16913704
ok... what do you need me to show or do and I will do just that this damn thing is taking to long....
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16913758
Is your page accessible online?
0
 

Author Comment

by:prowebinteractive
ID: 16913843
it is online, but this online software is for a second chance credit company and there is some info in there that cant be released, the page in question is password protected

I could give you a ling to be able to download the .js file


is that good ?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16913865
Send me the infos to the email address in my profile and trust my professionalism.
0
 

Author Comment

by:prowebinteractive
ID: 16913881
zp  @  arcor . de ?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16913902
Do not post email addresses in Questions. And yes, but of course without the blanks.

0
 

Author Comment

by:prowebinteractive
ID: 16914048
email sent
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 16915241
OK, I think we can reduce the complexity with a simple trick.
You need not to pass around the script var.
You have that account var in a hidden field value.
But that is all not neccessar.
Because your menu script needs the parameters which are already in the popup URL you can use the location.search property.
Change this:
it=s1.addItemWithImages(6,7,7,"Applicant",n,n,"",n,n,n,3,3,3,n,n,n,mystring + "clientDetails.php?accountNumber=",n,n,n,mystring + "https://domainname.com/secure/admin/clientDetails.php?accountNumber=",n,0,0,2,6,7,7,8,9,9,1,1,1,0);

To this:
it=s1.addItemWithImages(6,7,7,"Applicant",n,n,"",n,n,n,3,3,3,n,n,n,mystring + "clientDetails.php"+location.search,n,n,n,mystring + "https://domainname.com/secure/admin/clientDetails.php"+location.search,n,0,0,2,6,7,7,8,9,9,1,1,1,0);


But because I got the extra access over email and phone is points assigning to me not allowed by EE rules.
Ask please for points refund here: http:/Community_Support/askQuestion.jsp

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16915266
Sorry, I posted your URL here. I will ask for cleanup on Community Support.
And you can change your webMenu.js not to contain the host name.
Like this:
it=s1.addItemWithImages(6,7,7,"Applicant",n,n,"",n,n,n,3,3,3,n,n,n,mystring + "clientDetails.php"+location.search,n,n,n,mystring + "/secure/admin/clientDetails.php"+location.search,n,0,0,2,6,7,7,8,9,9,1,1,1,0);

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16921975
Thanks for removing the URLs.
I got the userid and password to access a closed user group account. Therefore is the equality rule broken. Please remove the points from this question.
And thanks for that ee-stuff thing! I did not know it. Opening the CV table I wondered why I am in second row with my 600 handled questions. When I sorted I saw that are 600 compared to 68000! from the top CV Venabili questions :)

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16922015
Oh, one more thing. The account and password was not alone the key. Talking on phone to asker I got the inside view what that all was for and could suggest a totally new approach.

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16925964
OK, when you say so :)
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question