Solved

retrieving a created variable in function

Posted on 2006-06-15
39
241 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
  • 21
  • 13
  • 3
39 Comments
 
LVL 28

Expert Comment

by:Pravin Asar
Comment Utility
You could use eval ('clientAccountNumber');


0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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 28

Expert Comment

by:Pravin Asar
Comment Utility
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
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
eval() is NOT necessary.
0
 

Author Comment

by:prowebinteractive
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You CANNOT retrieve PHP vars in JavaScript.
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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 28

Expert Comment

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

0
 
LVL 63

Expert Comment

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

Author Comment

by:prowebinteractive
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
doesnt work

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

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
why do things have to be made so damn complicated
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
:-)

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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That is a sequence problem, not a script problem.
0
 

Author Comment

by:prowebinteractive
Comment Utility
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
Comment Utility
Is your page accessible online?
0
 

Author Comment

by:prowebinteractive
Comment Utility
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
Comment Utility
Send me the infos to the email address in my profile and trust my professionalism.
0
 

Author Comment

by:prowebinteractive
Comment Utility
zp  @  arcor . de ?
0
 
LVL 63

Expert Comment

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

0
 

Author Comment

by:prowebinteractive
Comment Utility
email sent
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
OK, when you say so :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now