Solved

How to call a function inside another function?

Posted on 2009-03-28
5
1,431 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
Comment Utility
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
Comment Utility
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
Comment Utility
<!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
Comment Utility
Thanks for your expert help!
0
 
LVL 30

Expert Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

763 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

8 Experts available now in Live!

Get 1:1 Help Now