Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call Function Twice with Different Arguments?

Status
Not open for further replies.

nevadaflyfisher

Programmer
Mar 27, 2003
9
US
Good Morning,

I'd like to write one JavaScript function that will return formatted date/time values, with the format being different based on a passed argument. I'd then make two calls to the function from the same HTML page (rather than write a separate function for each different format.) What I've learned so far is that I can only make one call per page load. Is there a way to call the same function multiple times from the same page?

Here's my code so far:
Code:
<head>
<snip>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot; src=&quot;js/time.js&quot;></script>
</snip>
</head>

<body>
<snip>
<script type=&quot;text/javascript&quot;>document.write(getDateString(&quot;local&quot;))</script><br>
<script type=&quot;text/javascript&quot;>document.write(getDateString(&quot;utc&quot;))</script>
</snip>
</body>

Many thanks in advance,
Ken
 
What is wrong with:

Code:
<head>
<snip>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot; src=&quot;js/time.js&quot;></script>
</snip>
</head>

<body>
<snip>
<script type=&quot;text/javascript&quot;>
  var _html = '';
  _html += getDateString(&quot;local&quot;);
  _html += '<br>';
  _html += getDateString(&quot;utc&quot;);
  document.write(_html);
</script>
</snip>
</body>

It ought to do what you expect.

Jeff
 
your original method should work properly too. post your getDateString() function here to check for errors

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Hi Jeff,

I've done some additional troubleshooting with no success.

I've got the function stripped down to the bare minumum until I get it debugged:

Code:
function getDateString(zone)
{
var strMonth

switch (zone)
			 {
			 case &quot;local&quot; :
			 			{strMonth = &quot;A&quot;}
						break;
			 case &quot;utc&quot; :
			 			{strMonth = &quot;B&quot;}
						break;
			 default:
			 			alert(&quot;Hi!&quot;);
			 }				

getDateString = strMonth;
return(getDateString);
}

If I comment out the first function call, the second one executes just fine, and vice versa. Therefore, the problem seems to lie with multiple function calls from one HTML page.

If you have any thoughts, I'd love to hear them. In the meantime, I'll continue playing with it.

Thank you!
 
looks like you've got some vb bad habits

this line
getDateString = strMonth;

is effectively deleting your function from memory after it's first time executed since you're reassigning the function name to a variable. this should fix it:
Code:
function getDateString(zone)
{
var strMonth

switch (zone)
             {
             case &quot;local&quot; :
                         {strMonth = &quot;A&quot;}
                        break;
             case &quot;utc&quot; :
                         {strMonth = &quot;B&quot;}
                        break;
             default:
                         alert(&quot;Hi!&quot;);
             }                

return strMonth;
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
That was it, Jeff. Many, many thanks!

Obviously, it's time to head to B&N for a good JavaScript book. Any recommendations?
 
O'Reilly's &quot;Javascript: The Definitive Guide&quot; by Davaid Flanagan

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
I second that recommendation... that's what I &quot;cut my teeth&quot; on too.

Jeff
 
actually, i have a 3rd edition of that book that i don't use...4th edition is the latest but the 3rd is still an excellent reference. email me with your address and i'll mail it to you if you want.


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top