Link to home
Start Free TrialLog in
Avatar of jpfulton
jpfulton

asked on

JavaScript - Function, onload, local variable, return statement

I'm a javascript beginner. I'm taking a class at my college but my professor is a terrible teacher so I prefer to not ask him questions... which brings me here. I'm trying to understand functions, variable scope and returning a variable outside of a function. Please see code.

<html>
<head>
<script type="text/javascript">
function myDte()
        var d = new Date();
	curr_month = d.getMonth();
	curr_date = d.getDate();
	curr_year = d.getYear();
</script>
</head>

<body onload="myDte();">
<script type="text/javascript>
document.write(curr_month+"/"+curr_date+"/"+curr_year);
</script>
</html>

Open in new window


Obviously there's something fundamentally wrong here but it's a really simple concept. I want to be able to call a function (whether it's with "body onload" or some other call method). I tried messing with "return curr_date" (as well as the other variables) or with declaring curr_date (and the other variables) before the function, but can't figure it out. I've read a bunch of tutorials but just can't seem to nail down this specific question. Please help. I'm sure it's super easy.

P.S. If i run the series of statements outside of the function either within script tags either in the head or body it obviously works and I'm aware of that option but I'd really like to know how to accomplish this with a function. Thanks!
Avatar of sjklein42
sjklein42
Flag of United States of America image

Two bugs:

Missing close " on line 13

 <script type="text/javascript">

Open in new window


and you need curly brackets for the function:

function myDte()
{
        var d = new Date();
	curr_month = d.getMonth();
	curr_date = d.getDate();
	curr_year = d.getYear();
}

Open in new window

Avatar of jpfulton
jpfulton

ASKER

I should have put a little more effort in when typing that out. I actually have both of those correct in my original code... so that's not the problem unfortunately. Any other tips?
ASKER CERTIFIED SOLUTION
Avatar of sjklein42
sjklein42
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works like a charm:

<html>
<head>
<script type="text/javascript">

var curr_month;
var curr_date;
var curr_year;

function myDte() {
	var d = new Date();
	curr_month = d.getMonth();
	curr_date = d.getDate();
	curr_year = d.getYear();
	curr_month = ++curr_month;
	return curr_month, curr_date, curr_year;
        }
</script>
</head>

<body>

<script type="text/javascript">

myDte();
document.write(curr_month + "/" + curr_date + "/" + curr_year + "<br/>");

</script>

</body>

Open in new window


I kind of knew that declaring the variable outside of the function would ensure it stays global then returning all three of those variables back out, then actually just writing "myDte" was enough to call the function --- that i didn't know. I thought you actually needed some event to call a function, like onclick, onblur, onload etc etc.

Thanks for your help. I really appreciate it. I have another question regarding order of code etc. but that is for another thread. Points awarded.