Why not use Javascript handlers on the body element?

后端 未结 8 1448
有刺的猬
有刺的猬 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条回答
  •  萌比男神i
    2020-12-11 06:31

    There's nothing wrong with using the onload attribute in the element, provided:

    • you have complete control of the page
    • no other script that you use on the page currently or could have in the future will try and set an onload handler on the body element or the window object
    • you know your own mind and are happy to have a small piece of script in your mark-up.

    It's also worth noting that is a part of a formal standard (HTML 4) while window.onload is not, although it is implemented in all the major browsers going back many years.

提交回复
热议问题