Is ordering of <link rel=“stylesheet” …> and

2020-03-11 14:12发布

(I apologize if this is an extremely basic question. I'm a bit of an HTML noob.)

In HTML5, is the relative ordering within the <head> element, of elements having the form <link rel="stylesheet" type="text/css" ...> and elements having the form <script ...></script> at all significant, either semantically or performance-wise (or in some other way)?

For example, assuming a (possibly mythical) "fully HTML5-compliant browser", are there situations in which the following two snippets would produce "different results" (i.e., different appearance, or noticeable different performance, or different behavior, etc.)?

<!DOCTYPE html>
<html>
  <head>
    <!-- ... -->
    <link rel="stylesheet" type="text/css" href="foo.css"/>
    <script src="foo.js"></script>
    <!-- ... -->
  </head>
  <!-- ... -->
</html>

<!DOCTYPE html>
<html>
  <head>
    <!-- ... -->
    <script src="foo.js"></script>
    <link rel="stylesheet" type="text/css" href="foo.css"/>
    <!-- ... -->
  </head>
  <!-- ... -->
</html>

(<!-- ... --> denotes code that is both correct and common to both cases. IOW, the only difference between the two cases is the ordering of the <link...> and <script>...</script> elements within the <head>...</head> element.)

7条回答
女痞
2楼-- · 2020-03-11 14:36

In the general case, the order should be assumed to be significant. Because it can be. JavaScript code may, for example, try to access a link element, and then it matters whether the link element appears before the script element (in which case it exists in the DOM when the script is executed).

查看更多
倾城 Initia
3楼-- · 2020-03-11 14:37

In terms for performance... include CSS files in the header and js files at the bottom of the page...

In terms of order the order of css files and js files matter... in css file if the same rule is present in multiple files the later once will get precedence... in js file the depended files should be included first

查看更多
爷的心禁止访问
4楼-- · 2020-03-11 14:38

It is always better you include style sheet in the header and the scripts at the bottom because, users have to wait till the JavaScript finishes loading, tags block parallel downloads.

please look the following links\ Is the recommendation to include CSS before JavaScript invalid?

查看更多
Evening l夕情丶
5楼-- · 2020-03-11 14:45

To increase the performance of html page load, it is recommended that you use Javascript tag in the end of the body and css rule in the head. Precedence need to be maintain when other script or css is referenced in one of the file which should be there before the file which is referencing it.

We can consider a simple example that. jquery.js file always be at top of the othes js files because, other files rely on that jquery file.

It should be noted that scripts are interpreted, so there ordering is important.

查看更多
我只想做你的唯一
6楼-- · 2020-03-11 14:46

No, there is no difference.

The only thing you should consider is putting your <script> tags at the bottom of the <body>. For more information regarding this issue read this article by Yahoo.

查看更多
▲ chillily
7楼-- · 2020-03-11 14:46

Only one I can think of is the meta tag for IE...

<meta http-equiv='X-UA-Compatible' content='IE=edge'>

For IE to make use of that one, it has to be the first item in the head (AFAIK)...

查看更多
登录 后发表回答