Dombo
Dombo

Reputation: 1

Check date on page load

I would like to get the following script to run when the window first loads.

    var now = newDate();
    var years = now.getFullYear();
    var month = now.getMonth();
    var days = now.getDate();

    month = month +1;

    var today = days + "." + month + "." + years;
    var checkdate = 11.05.2020;

    if (today >= checkdate)
    {
        alert("Today is the day");
        document.location.href="today.html";
    }
    else
    {
        alert("Today is " + today);
    }

Please help.

Upvotes: 0

Views: 735

Answers (2)

leo_lo
leo_lo

Reputation: 26

run the code after the page has been loaded

$(function() {
    // ...Code goes here
});

or

$(document).ready(function() {
   // ...Code goes here
});

Upvotes: 0

Tranquillity
Tranquillity

Reputation: 367

Try this.

function myFunction() {
  var now = new Date();
  var years = now.getFullYear();
  var month = now.getMonth();
  var days = now.getDate();
  var today = days + "." + month + "." + years;
  var checkdate = "11.05.2020";
  if (today >= checkdate) {
    alert("Today is the day");
    document.location.href = "today.html";
  } else {
    alert("Today is " + today);
  }

}
<!DOCTYPE html>
<html>

<head>
  <script>
  </script>
</head>

<body onload="myFunction()">

  <h1>Hello World!</h1>


</body>

</html>

Upvotes: 1

Related Questions