Where should I declare JavaScript files used in my page? In <head></head> or near </body>?

后端 未结 12 943
后悔当初
后悔当初 2020-11-27 15:07

I was reading a tutorial and the author mentioned to include Javascript files near closing body tag () in HTML.

I was wonderi

12条回答
  •  自闭症患者
    2020-11-27 15:47

    By putting them in the you force the browser to download the files before it can render a page. That causes the perceived load time to slow down.

    By placing them in the footer, right before the closing body tag, the browser will not load them until it reaches that point in the parsing of the HTML. That means that the scripts will run later in the page load process but will not block the asset download and rendering process.

    Which works best is up to you and how you develop your code.

提交回复
热议问题