Solved

JavaScript - Function, onload, local variable, return statement

Posted on 2011-02-15
4
756 Views
Last Modified: 2012-05-11
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
Comment
Question by:jpfulton
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34897874
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
 

Author Comment

by:jpfulton
ID: 34897890
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
 
LVL 16

Accepted Solution

by:
sjklein42 earned 125 total points
ID: 34897917
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
 

Author Comment

by:jpfulton
ID: 34898824
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now