简体   繁体   English

在html文件中编写脚本的正确方法

[英]right way to write script in html file

I have written a script in HTML, but i don't know the best way to place script in HTML. 我已经用HTML编写了脚本,但是我不知道将脚本放入HTML的最佳方法。

1.In Head 1.在头

<html>
    <head>
        <script></script>
    <head>
    <body></body>
</html>

2. After Body 2.后身体

<html>
    <head><head>
    <body></body>
    <script></script>
</html>

3. After Html 3. HTML之后

<html>
    <head><head>
    <body></body>
</html>
<script></script>

According to Yahoo performance team the <script> should be placed at the bottom of the page. 根据Yahoo绩效小组的说法, <script>应该放在页面底部。 But the problem caused by scripts is that they block parallel downloads which might slow the page. 但是由脚本引起的问题是它们阻止并行下载,这可能会使页面变慢。

Another suggestion is to use async and defer . 另一个建议是使用asyncdefer The async and defer HTML attributes let you control when and how JavaScript files are downloaded and executed. asyncdefer HTML属性使您可以控制何时以及如何下载和执行JavaScript文件。 Using these attributes, you can reduce the amount of time users spend waiting on JavaScript without having to change your scripts. 使用这些属性,可以减少用户等待JavaScript所花费的时间,而不必更改脚本。

Reference 参考

The Right way for writing Script is below. 下面是编写脚本的正确方法。

<html>
    <head>
        <script></script>
    <head>
    <body></body>
</html>

2 and 3 are technically incorrect but probably work in browsers. 2和3在技术上是不正确的,但可能在浏览器中有效。

For performance in website write script like below. 为了提高网站性能,请编写如下脚本。

<html>
    <head><head>
    <body>
        <script></script>
    </body>
</html>

Do like this: 这样做:

<html>
    <head><head>
    <body></body>
    <script></script>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM