1

I want my function "thing()" to run automatically from the page load. At the moment it is activated by a button.

HTML

<button onclick="thing()">Greeting</button>

Javascript

function thing() {
  var greeting;
  var time = new Date().getHours();
  if (time < 10) {
    greeting = "Good morning";
  } else if (time < 20) {
    greeting = "Good day";
  } else {
    greeting = "Good Evening";
  }
  document.getElementById("address").innerHTML = greeting;
}

I am very new to java script so as simply please.

1

1 Answer 1

0
window.onload = thing();

will run the function automagically!!

Sign up to request clarification or add additional context in comments.

3 Comments

should be more correct writing window.addEventListener('load', thing); as there may be more than one function to run
window.onload = thing(); Didnt Work But window.addEventListener('load', thing); did. Thanks
It doesn’t work because windows.onload = thing() assignes the result of calling thing() to window.onload.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.