Javascript examples for DOM Event:onload
Declare function inside the body tag and window onload event handler
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript"> window.onload = function () {/*from ww w. ja va 2 s . com*/ var f = function(caller) { console.log('test'); }; var myButton = document.getElementById('myButton'); myButton.addEventListener('click', function(){ f(this); }); }; </script> </head> <body> <button id="myButton" type="button">A button</button> </body> </html>