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
Solved

JavaScript - Function, onload, local variable, return statement

Posted on 2011-02-15
4
761 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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

828 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