2

Hi I am new to Javascript and am trying to create a function that checks two dates. I have read it is useful to put the JS in the head part of the document, but this is not returning anything. I am also new to stackoverflow so I hope I did this correctly. :) Does anyone see the error?

<!DOCTYPE html>
<html>
<head>

var myDate = new Date(); // Your timezone!
var myEpoch = myDate.getTime()/1000;
var deadline = '1341596750.000';

document.write(myEpoch);
document.write("<br>",deadline);

if (myEpoch < deadline) {
  document.write("<p>Just in time!</p>");
} else {
  document.write("<p>Too late!</p>");
}

</head>
<body>

<br><br><br><br>http://www.epochconverter.com/
</body>
</html>
5
  • 1
    Where to start.... first, the code needs to live between <script> tags, the document.write will be prepended to the body and... well I'll stop there. Commented Jul 6, 2012 at 20:36
  • @TheZ: Actually, document.write works fine in the <head> :-P Commented Jul 6, 2012 at 20:36
  • @Rocket Sure, if prependeding is what you want, which in this case it is Commented Jul 6, 2012 at 20:40
  • 1
    @rocket: yeah, but you'll end up with 12345678<br><p>Just in time!</p> in the head, which is not exactly valid. Commented Jul 6, 2012 at 20:44
  • thank you for the help guys. and thanks for liking my question. my points keep going up. YAY :) Commented Jul 6, 2012 at 20:48

1 Answer 1

3

You have to mention it's a script using <script>. Also you shouldn't output DOM in the <head> like you are doing with document.write. Manipulate the DOM like this instead:

    <head>
    <script type="text/javascript">

    var myDate = new Date(); // Your timezone!
    var myEpoch = myDate.getTime()/1000;
    var deadline = '1341596750.000';

    document.write(myEpoch);
    document.write("<br>",deadline);

    if (myEpoch < deadline) {
      document.getElementById("useme").innerHTML("Just in time!");
    } else {
      document.getElementById("useme").innerHTML("Too late!");
    }
    </script>
    </head>
<body>

<p id="useme"></p>
Sign up to request clarification or add additional context in comments.

8 Comments

Ah!! I didn't see i left out the JS tags. Thank you!!
@JohnDoe Don't forget the <facepalm></facepalm> tag too
that didn't do anything. what do facepalm tags do?
@JohnDoe: That was supposed to be a joke =/
haha thanks for liking my question. this site is actually pretty cool :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.