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

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

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

17条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 02:13

    A few things:

    1. It's completely valid code-wise.
    2. It's completely unrecommended.

    Doing this slows down your page load considerably as the JavaScript code must execute before any of the rest of the page can render. If you're doing a lot of work in that JavaScript code, your browser could hang. You should try to (whenever possible) load your JavaScript code dynamically and at the end of your page (preferably before the tag).

    Purchase and read High Performance JavaScript. It will change the way you write JavaScript code.

提交回复
热议问题