Solved

JavaScript - Function, onload, local variable, return statement

Posted on 2011-02-15
4
772 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

717 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