At end of this code (full code is here)
function addSongs() {
var song1 = document.getElementById("song1");
var song2 = document.getElementById("song2");
var song3 = document.getElementById("song3");
song1.innerHTML = "Blue Suede Strings, by Elvis Pagely";
song2.innerHTML = "Great Objects on Fire, by Jerry JSON Lewis";
song3.innerHTML = "I Code the Line, by Johnny JavaScript";
}
window.onload = addSongs;
You will see addSongs didn’t used with ().
If i change it as
window.onload = addSongs();
It doesn’t works. Why?
when you have
addSongs(), it’s telling Javascript immediately execute that function and assign the return value of that call to the onload handler.Without the
(), it tells the JS engine to assign the function itself to onload.e.g. if you had something like
which is the same as doing
and at some point the JS engine will try to execute a function named “hello”, whenever the onload handler is triggered.