Add event handler for body.onload by javascript within <body> part

后端 未结 5 834
深忆病人
深忆病人 2020-12-13 18:10

We want to include a maps from Google Maps API in our document. The documentation tells to initialize the map with a function called by the onload() event of the body.

5条回答
  •  清歌不尽
    2020-12-13 18:26

    body.addEventListener("load", init(), false);
    

    That init() is saying run this function now and assign whatever it returns to the load event.

    What you want is to assign the reference to the function, not the result. So you need to drop the ().

    body.addEventListener("load", init, false);
    

    Also you should be using window.onload and not body.onload

    addEventListener is supported in most browsers except IE 8.

提交回复
热议问题