0

Unable to get vanilla JavaScript syntax or maybe I have brainfreeze.

const xhttp = new XMLHttpRequest();

xhttp.onload = function() {
console.log('hello');
}
xhttp.open("GET", "my data.txt");
xhttp.send();

The onload function is defined and called immediately, why don't we need to call it seperately after defining like other normal functions?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
rachit3dev
  • 81
  • 9
  • 1
    Your console.log is missing one single qoute in the end – Harshit Rastogi Sep 03 '21 at 06:39
  • It is NOT called immediately. It is a callback eventHandler and is executed AFTER the send on successful call. Please [RTM](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/load_event) – mplungjan Sep 03 '21 at 06:46
  • And please also read [Event handlers](https://developer.mozilla.org/en-US/docs/Web/Events/Event_handlers) – mplungjan Sep 03 '21 at 06:50

0 Answers0