Why not use Javascript handlers on the body element?

后端 未结 8 1465
有刺的猬
有刺的猬 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:31

    Disregarding the issues of whether inline event handler attributes are a wrongness for a moment, the onload event is a poor place to put an autofocuser, since it only fires when the whole page is loaded, including images.

    This means the user will have to wait longer for the autofocus to occur, and if the page takes quite a while to load they may already have focused elsewhere on the browser page (or chrome, such as the address bar), only to find their focus stolen halfway through typing. This is highly irritating.

    Autofocus is a potentially-hostile feature that should be used sparingly and politely. Part of that is reducing the delay before focusing, and the easiest way to do that is a script block directly after the element itself.

    
    
    

提交回复
热议问题