最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Why not use Javascript handlers on the body element? - Stack Overflow

programmeradmin8浏览0评论

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

<body onLoad="document.getElementById('<id>').focus();">

Ben Scheirman replies (without further explanation):

Any Javascript book will tell you not to put handlers on the body element like that

Why would this be considered bad practice? In Espos answer, an 'override' problem is illustrated. Is this the only reason, or are there any other problems? Compatibility issues?

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

<body onLoad="document.getElementById('<id>').focus();">

Ben Scheirman replies (without further explanation):

Any Javascript book will tell you not to put handlers on the body element like that

Why would this be considered bad practice? In Espos answer, an 'override' problem is illustrated. Is this the only reason, or are there any other problems? Compatibility issues?

Share Improve this question edited Jun 4, 2020 at 19:24 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 28, 2010 at 11:19 Alexander TorstlingAlexander Torstling 18.9k7 gold badges63 silver badges77 bronze badges
Add a ment  | 

8 Answers 8

Reset to default 8

Using onLoad is being less and less mon 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 plete 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...... }

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

  • you have plete 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 <body onload="..."> 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.

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.

<input id="x">
<script type="text/javascript">
    document.getElementById('x').focus();
</script>

I suppose this has to do with other javascript files also. If some of your javascript files contain a handler for body onload, and if you supply an inline body onload handler in your page then the handler inside the script won't be executed.

Well, '<id>' is an invalid id value if you ask me. I wouldn't use any characters like this.

And for good practice, it's better to use window.onload IMO and place it in the <head> tag. OR Better yet, you can move it to a .js file and cache it for speed.

As a more non-javascript related answer, the presentation layer of you application could be in the form of templates or even nested templates (dreamweaver or Master Pages etc) where having specific code in the body tag may not be required or conflict with other code required when the template is "inherited"

I can see no other reason than the possible later override, which would lead to your code never being executed. Also, obtrusive javascript is not in very high regard anymore.

You should instead assign a function listening to the event. You can do it like this:

function onloadFocus() {
    document.getElementById('<id>').focus();
}

if (window.addEventListener) {      
    window.addEventListener('load', function(e) {onloadFocus();}, false);} 
else if (window.attachEvent) {
    window.attachEvent('onload', function(e) {onloadFocus();}); 
}

...or rely on a javascript framework, such as jquery, that would provide the same functionality for you.

Another way to look at it is using an addEventListener instead of using it as an html code try JS:

<body onLoad="document.getElementById('<id>').focus();">

    <body>

    <script>

     document.body.addEventListener('load', funcLoad);

     function funcLoad(){

     document.getElementById('<id>').focus();

    }

    </script>
</body>

Try it out, it might work better than other solutions.

发布评论

评论列表(0)

  1. 暂无评论