I am using Tampermonkey for some webpage manipulation and automation. For this, I add an event listener to the window "load" event. When I define the event handler as an explicit function, it works smoothly and as expected, i.e. the function is called whenever the relevant page is loaded.
window.addEventListener("load", run());
function run() {
// do something
}
However, when I define the handler inline as follows
window.addEventListener("load", function (){
// do something
});
then I need to refresh / reload the page several times before the function gets executed.
What is the explanation to have such very different behaviour? I would have expected no difference whether I define the function "explicitly" as in the first example, or "inline" as in the second one. As I have not noticed this before I don't even know whether this is a Tampermonkey issue or a Javascript one.