Solved

How to call a function inside another function?

Posted on 2009-03-28
5
1,439 Views
Last Modified: 2012-05-06
Is it possible to call another function inside another one when using it in an href? See below.
My second function "getJulianDate()" just prints the name out (ie.. "getJulianDate()".
<a href="javascript:popItem(4,getJulianDate());">Test Link</a>

Open in new window

0
Comment
Question by:greddin
  • 2
  • 2
5 Comments
 
LVL 30

Assisted Solution

by:third
third earned 100 total points
ID: 24012460
it should work fine. see sample below.

though i suggest you use this format (i.e. use onclick and always return false so href will not be executed.)

<a href="#" onclick="popItem(4,getJulianDate());return false;">Recommended Test Link</a>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Third Santor</title>

<script>

function getJulianDate(){

  return 'julian';

}
 

function popItem(num, str){

  alert(str);

}

</script>

</head>

<body>

<a href="javascript:popItem(4,getJulianDate());">Test Link</a><br /><br />
 
 

<a href="#" onclick="popItem(4,getJulianDate());return false;">Recommended Test Link</a>

</body>

</html>

Open in new window

0
 
LVL 15

Expert Comment

by:fsze88
ID: 24012795
try using eval function may be better
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Third Santor</title>

<script>

function getJulianDate(){

  return 'julian';

}

 

function popItem(num, str){

  eval(str);

}
 

function getJulianDate2(){

  alert('do somethings here');

}
 

</script>

</head>

<body>

<a href="javascript:popItem(4,'getJulianDate2()');">Test Link</a><br /><br />

 

 

<a href="#" onclick="popItem(4,'getJulianDate2()');return false;">Recommended Test Link</a>

</body>

</html>

Open in new window

0
 
LVL 15

Accepted Solution

by:
fsze88 earned 400 total points
ID: 24012926
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Third Santor</title>
<script>
function getJulianDate(){
  return 'julian';
}
 
function popItem(num, str){
  var returnvar;
  eval('returnvar = ' + str);
  alert(returnvar);
}

function getJulianDate2(){
//  alert('do somethings here');
  return 'abc';
}

</script>
</head>
<body>
<a href="javascript:popItem(4,'getJulianDate2()');">Test Link</a><br /><br />
 
 
<a href="#" onclick="popItem(4,'getJulianDate2()');return false;">Recommended Test Link</a>
</body>
</html>
0
 

Author Closing Comment

by:greddin
ID: 31563978
Thanks for your expert help!
0
 
LVL 30

Expert Comment

by:third
ID: 24015153
Welcome. Using eval on this case though is no way better.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now