Why not use Javascript handlers on the body element?

后端 未结 8 1434
有刺的猬
有刺的猬 2020-12-11 05:50

As an answer to the question of \'How do you automatically set the focus to a textbox when a web page loads?\', Espo suggests using



        
8条回答
  •  醉梦人生
    2020-12-11 06:20

    Using onLoad is becoming less and less common because callbacks can't be stacked using this method, i.e. new onload definitions override the old ones.

    In modern frameworks like jQuery and its .load(), callbacks can be stacked and there are no conflicts when using different scripts, plugins, etc. on the same page.

    Also, it is widely regarded good practice to keep the markup separate from the code, so even if one would want to use onload (which is perfectly okay if you control the complete environment and know what you're doing) one would attach that event on the scripting side either in the head or a separate javaScript file:

    window.onload = function() { document.getElementById...... }
    

提交回复
热议问题