View Full Version : Newbie question on simple script

01-21-2009, 12:17 AM
Im trying to get started on js, and really want to write a successful script, but this isnt working.... anyone know what is wrong?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<script language="javascript">

function time () {

var today = new Date();
var hours = today.getHours();
var minutes = today.getMinutes();
var result;

result = hours + ":" + minutes
return "The time is" + result;




<script language="javascript">
document.write (time(result));



01-21-2009, 02:24 AM
Im trying to get started on js, and really want to write a successful script, but this isnt working.... anyone know what is wrong?Letís see:

Youíre trying to pass an undeclared variable (i.e., result) in your second script element. Declare it or remove it. If you declare it, youíll have to undeclare it within the time function since you canít declare the same variable twice.

Youíre passing a parameter (i.e., time(result)) to the time function, but you havenít assigned it a name. (You would do that with something like function time(parameterName).) Remove the parameter or assign it a name.

The document.write method doesnít work in XHTML documents served with an XML MIME type. Use W3C DOM methods to add the text or use HTML instead of XHTML or do both.
Your XHTML document doesnít comply with Appendix C of the XHTML 1.0 spec. which is required if serving XHTML 1.0 documents with an HTML MIME type. Use HTML or revise your code so that it adheres to Appendix C.

Below is some code that implements the previous suggestions mentioned above. Since you claim to be a newbie, Iíll note that firstChild property refers to the first child node of the p element. The first child node happens to be a text node. To get or set the value (i.e., text) of a text node, you can use the data property.

<!doctype html public "-//W3C//DTD HTML 4.01//EN">

<html lang="en-Latn">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Script-Type" content="application/ecmascript">
<style type="text/css" media="all">
* { margin: 0; padding: 0; }
html { background: white; color: black; }
p { margin: 1em; }
<script type="text/ecmascript">
function script() {
var date = new Date();
var paragraph = "The time is " + date.getHours() + ":" + date.getMinutes() + ".";
document.getElementById("content").firstChild.data = paragraph;

<body onload="script();">

<p id="content">If the time isnít shown here, the script failed.</p>


Verified in Mozilla Firefox 3.0.5.

01-21-2009, 05:48 PM
thanks! i sorta get what you said. i thought (time(result)) would write both time and result.

but result is part of the time function.

i was looking for where "result" would need to be written somewhere. "return" is the final piece that is relayed to "time".

and "time" is what is spit out in the body.

i opened a new HTML file. sounds like if i want to or need to use xhtml, i have to do some stuff with DOM. which I dont know what that is yet. thanks for the help.