Is it bad practice to embed JavaScript into the body of HTML?

前端 未结 17 2481
梦谈多话
梦谈多话 2020-12-13 01:44

A team that I am working on has gotten into the habit of using

17条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 02:05

    I assume that your team is doing this either because they want to insert a script dynamically, or that they are writing a script that will fire at page load.

    I wouldn't say there's anything wrong with doing this when ABSOLUTELY NECESSARY, (as long as it's in a CDATA block), but outside of that, I would recommend to your team that they use a script library like Prototype or jQuery, and keep the scripts external to the page. This is usually cleaner, and libraries will sometimes force a bit of cleanliness to the code, which I would bet isn't happening currently.

    I also wouldn't run any time-consuming functions in inline script tags, as these happen on page load, and as Jason stated above, could slow the load of the page. All script libraries have neat functions that allow you to do things on load of the page, and will give you the option of when in the page load to fire them, such as after the DOM is loaded.

提交回复
热议问题