Solved

How to call a function inside another function?

Posted on 2009-03-28
5
1,449 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I'm a Human captcha checkbox 13 39
Send form to asp server side 6 26
Sending ASP to server side 8 28
Aspx calendar pop up control 3 25
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

685 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