Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Newbie question on simple script

    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">
    <head>




    <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>

    </head>



    <body>


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

    </script>


    </body>
    </html>

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,304
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by dgbenner View Post
    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.

    Code:
    <!doctype html public "-//W3C//DTD HTML 4.01//EN">
    
    <html lang="en-Latn">
    	<head>
    
    		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    		<meta http-equiv="Content-Script-Type" content="application/ecmascript">
    		<title>Demo</title>
    		<style type="text/css" media="all">
    			* { margin: 0; padding: 0; }
    			html { background: white; color: black; }
    			p { margin: 1em; }
    		</style>
    		<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;
    			}
    		</script>
    
    	</head>
    	<body onload="script();">
    
    		<p id="content">If the time isn’t shown here, the script failed.</p>
    
    	</body>
    </html>
    Verified in Mozilla Firefox 3.0.5.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •