JavaScript in <head> or just before </body>?

前端 未结 6 2205
攒了一身酷
攒了一身酷 2020-11-28 06:14

I am about to embark on a new web project and I plan to put some JavaScripts in the and also some before , using the foll

6条回答
  •  旧巷少年郎
    2020-11-28 06:35

    One of the reasons you'd want to put scripts before the is if they manipulate the DOM without user interaction, so you'll need the DOM to be loaded in order to be manipulated. Another way to do that is to add an event listener and run the scripts when the page has loaded, but this will require additional code, which might get complicated if you have a lot of scripts, especially ones you haven't written yourself. Putting them at the end of the page also will speed up page load, though in the case of DOM manipulating scripts you might get some not-so-pretty results from that.

提交回复
热议问题