Baxtex Posted April 20, 2011 Share Posted April 20, 2011 So, here is my code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Min javascript sida</title></head><body><h1>hej</h1><script type="text/javascript">document.write("<p>"+date()+"</p>"):</script></body></html> It's meant to to show what time it is, but it doesn't work. i Just see my headline. Can anyone see what i have done wrong? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 20, 2011 Share Posted April 20, 2011 Javascript doesn't have a date function, it has a date object that you can create and then use various functions to output the date in different ways.http://www.w3schools.com/js/js_obj_date.asp Link to comment Share on other sites More sharing options...
Baxtex Posted April 25, 2011 Author Share Posted April 25, 2011 Well, i tried this example: http://www.w3schools.com/js/tryit.asp?filename=tryjs_writeOn the site, it also says: Note: Try to avoid using document.write() in real life JavaScript code. The entire HTML page will be overwritten if document.write() is used inside a function, or after the page is loaded. However, document.write() is an easy way to demonstrate JavaScript output in a tutorial.So what does this mean? How do "real life code" look like? Link to comment Share on other sites More sharing options...
ShadowMage Posted April 25, 2011 Share Posted April 25, 2011 Well, i tried this example: http://www.w3schools.com/js/tryit.asp?filename=tryjs_writeOn the site, it also says: Note: Try to avoid using document.write() in real life JavaScript code. The entire HTML page will be overwritten if document.write() is used inside a function, or after the page is loaded. However, document.write() is an easy way to demonstrate JavaScript output in a tutorial.So what does this mean? How do "real life code" look like?Remember, JavaScript is case sensitive."Real life code" will use DOM functions like getElementById to get references to elements and set their values or innerHTML. So to follow this convention, you would assign the date to a variable instead of just printing it. Then use the variable to insert the date into an element already created on the page. Link to comment Share on other sites More sharing options...
jeffman Posted April 25, 2011 Share Posted April 25, 2011 This is a more real-life version of the code on that page: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <title></title> <script type="text/javascript"> function init () { var myDate = Date(); document.getElementById("output").innerHTML = myDate.toString(); } window.onload = init; </script> </head> <body> <h1>My First Web Page</h1> <p id="output"></p> </body></html> (Don't worry about my putting a lot of effort into that. I just made a few simple changes to a template doc that I keep around.) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.