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.

Andy
  • 61,948
  • 13
  • 68
  • 95
lesquishy
  • 137
  • 1
  • 1
  • 6

1 Answers1

0
window.onload = thing();

will run the function automagically!!

Akshay Khandelwal
  • 1,570
  • 11
  • 19
  • 2
    should be more correct writing `window.addEventListener('load', thing);` as there may be more than one function to run – Jack May 12 '15 at 11:53
  • `window.onload = thing();` Didnt Work But `window.addEventListener('load', thing);` did. Thanks – lesquishy May 12 '15 at 12:08
  • It doesn’t work because `windows.onload = thing()` assignes the *result* of calling `thing()` to `window.onload`. – bfontaine Nov 21 '15 at 21:14