Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 789
  • Last Modified:

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!
0
jpfulton
Asked:
jpfulton
  • 2
  • 2
1 Solution
 
sjklein42Commented:
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

0
 
jpfultonAuthor Commented:
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?
0
 
sjklein42Commented:
Also you need to declare these variables in outer block (right after the first <script> line):

var curr_month
var curr_date
var curr_year

But the fundamental problem is that myDte is called AFTER the document.write was called, so the document.write variables have not been set yet.

You need to realize that the onload callback is only called after ALL the body has been constructed, which included doing the document.write.

0
 
jpfultonAuthor Commented:
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now