[removed] seems to trigger before the DOM is loaded (JavaScript)

后端 未结 5 1483
我在风中等你
我在风中等你 2020-11-28 13:45

I am having trouble with the window.onload and document.onload events. Everything I read tells me these will not trigger until the DOM is fully loa

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 14:34

    It's important to understand that () is an operator, just like + or &&. () Takes a function and calls it.

    So in that case, doThis is a function object, and () is the operator that calls the function. doThis() combined to together calls doThis, executes it, and evaluates to the return value of doThis

    So window.onload = doThis() is basically assigning the return value of doThis to window.onload

    And thus, to fix that, you need to reference the function itself, not call it.

    Do window.onload = doThis

提交回复
热议问题