HTML 可以包含两个 HEAD 标签吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24157836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:01:26  来源:igfitidea点击:

Can HTML contain two HEAD tags

htmljsp

提问by sureshd

In my web application I got Header.jsp file which contains default header contents. Im including it in all other pages using jsp:include tag inside body tag of each individual pages.

在我的 Web 应用程序中,我得到了包含默认标题内容的 Header.jsp 文件。我在所有其他页面中使用 jsp:include 标签将它包含在每个单独页面的 body 标签中。

Header.jsp contains its own HEAD tag to specify default meta tags, link stylesheets, scripts and some HTML elements. At the same time I will have another set of HEAD tag in all other individual pages to define title, page specific script and stylesheets.

Header.jsp 包含它自己的 HEAD 标记来指定默认元标记、链接样式表、脚本和一些 HTML 元素。同时,我将在所有其他单独页面中使用另一组 HEAD 标记来定义标题、页面特定脚本和样式表。

For example:

例如:

Header.jsp

头文件.jsp

<head>
   <link rel="shortcut icon" href="<%=request.getContextPath()%>/images/favicon.ico" type="image/x-icon" />
   <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
   <script src="js/jquery.js"></script>
   <link rel=stylesheet type="text/css" href="dashboard.css" >
</head>
<h2>Dashboard</h2>

Main.jsp

主程序

<!DOCTYPE html> 
<html>
   <head>
      <title>Main page</title>
      <script src="main.js"></script>
   </head>
   <body>
      <jsp:include page="Header.jsp" flush="true" />
      .....
      other HTML contents specific to main page
      .....
   </body>
</html>

whether it is valid to do like this?

这样做是否有效?

采纳答案by mvillar

It's not valid according to the standard

根据标准无效

Relevant part:

相关部分:

4.2.1 The head element

Categories: None.

Contexts in which this element can be used: As the first element in an html element.

4.2.1 头部元素

类别:无。

可以使用此元素的上下文:作为 html 元素中的第一个元素。

Your second <head>element wouldn't be the first element in the html document.

您的第二个<head>元素不会是 html 文档中的第一个元素。

回答by dkellner

The short answer is YES.

简短的回答是肯定的。

It's not a good solution but it will absolutely work.

这不是一个好的解决方案,但绝对有效。

People usually answer these questions in theory, like "no, because it's not valid by the standards". That's right, it's not. Future browsers might not support it, some source parsers may get confused, HR/IT experts checking out your portfolio may think you know less than Jon Snow, and all sorts of bad things. In theory. But it does happen out there in the real world, and browsers are not stupid: they knowwhat you mean, they willtake both head tags and work as expected.

人们通常会在理论上回答这些问题,比如“不,因为按照标准它是无效的”。没错,不是。未来的浏览器可能不支持它,一些源解析器可能会感到困惑,检查你的投资组合的 HR/IT 专家可能认为你知道的比 Jon Snow 少,以及各种糟糕的事情。理论上。但它确实发生在现实世界中,浏览器并不愚蠢:它们知道你的意思,它们同时使用头部标签并按预期工作。

And it's not just by chance.
They have very good reasons:

这不仅仅是偶然的。
他们有很好的理由:

1. Head tag is optional.(see notes below the article!)
Browsers accept headtag-like contents even outside it, so in effect they ignore the tag itself completely. And if they ignore one, they will probably ignore several, too.

1. 头部标签是可选的。(请参阅文章下方的注释!)
浏览器甚至在它之外也接受类似 headtag 的内容,因此实际上它们完全忽略了标签本身。如果他们忽略一个,他们可能也会忽略几个。

2. Visitors are precious.
Browsers want you to enjoy your time. They want to show you the best possible page they can compose out of the mess they've got. It's their intention,they want to show you that the internet works, not teach you how bad your favorite website is. If they can find out what the html code wants to express (and there's no deadly ambiguity in the structure), they will do their best to fix the page.

2. 访客是宝贵的。
浏览器希望您享受您的时间。他们想向您展示他们可以从他们所拥有的混乱中撰写的最佳页面。 这是他们的意图,他们想向您展示互联网是有效的,而不是告诉您您最喜欢的网站有多糟糕。如果他们能找出 html 代码想要表达的内容(并且结构中没有致命的歧义),他们将尽最大努力修复页面。

3. Bad markup tolerance is a thing.
Browsers are not just patient and forgiving, but sometimes they do acrobatic moves to get your stuff working. Look at this horrible mess:

3. 糟糕的标记容忍度是一回事。
浏览器不仅耐心和宽容,而且有时他们会做一些杂技动作来让您的东西正常工作。看看这可怕的混乱:

<!-- no doctype! -->
<!-- no HTML tag! we're all gonna die! -->
<head>
    <style>
        body {background:#002233;}
    </style>
</head>
<head><!-- let's twist again! -->
    <style>
        body {color:white}
    </style>
<!-- we didn't even close the second one!! -->

See this text?<br>
With the background AND color properly set?<br>
<br>
Your browser's quite a badass.

About browser tolerance, here's a lot morewith super-ugly examples - make sure you forget everything you've seen when you're back!)

关于浏览器的容忍度,这里有更多超级丑陋的例子 - 确保你回来后忘记你看到的一切!)

So yes, of course, the principle is "be a good friend of your browser", no matter how cleverly it fixes your mistakes. But if you wake up in a dark dungeon with hungry lions around and your only way out is using two <head> tags - well, don't hesitate! It's notbroken syntax, it's nota serious violation of HTML5 rules - it's no more than a convenient cheat. And don't fall for the widespread myth that non-standard, non-tidy sites prosper a lot worse: people usually just don't know for sure and want to stay on the safe side. Typically they are the ones who describe hell as a place where validator-failing web authors go.

所以是的,当然,原则是“成为浏览器的好朋友”,无论它多么巧妙地修复了你的错误。但是,如果您在黑暗的地牢中醒来,周围有饥饿的狮子,而您唯一的出路就是使用两个 <head> 标签 - 好吧,不要犹豫!它没有破坏语法,也没有严重违反 HTML5 规则——它只不过是一种方便的作弊方式。并且不要相信非标准、不整洁的网站发展得更糟糕的普遍神话:人们通常只是不确定并希望保持安全。通常,他们将地狱描述为验证器失败的网络作者去的地方。

TLDR: In practice, two head tags work.

TLDR:实际上,两个头部标签都有效。

Now please have only one if possible.

如果可能的话,现在请只有一个。



ADDITIONAL NOTES

补充说明

As @StanislavBerkov pointed out, both W3C and MDN suggests that the HEAD tag is implied, meaning it's probably better to just leave the headtag entirely. I would not recommend this approach if you have the standard option of using only one of it, but having none is apparently better than having two. The documentation is not very clear around the topic so make sure you test everything in the major browsers - but again, in practice, you'll face no issues.

正如@StanislavBerkov 指出的那样,W3C 和 MDN 都建议 HEAD 标签是隐含的,这意味着head完全保留标签可能更好。如果您有只使用其中一个的标准选项,我不会推荐这种方法,但没有一个显然比拥有两个好。该主题的文档不是很清楚,因此请确保您在主要浏览器中测试所有内容 - 但同样,在实践中,您不会遇到任何问题。

回答by heinkasner

Good response @Gwenc37. You can have any tags in any other tags, but it is always best to keep to the W3C standards and specifications. Later on in the project you might get to a point where your HTML does not parse correctly in a browser or even worse breaks.

良好的反应@Gwenc37。您可以在任何其他标签中使用任何标签,但最好始终遵守 W3C 标准和规范。在项目的后期,您可能会遇到 HTML 无法在浏览器中正确解析甚至更糟的情况。

To be safe, rather keep to the W3C standards. This way you cannot go wrong. Hope this helps.

为了安全起见,最好遵守 W3C 标准。这样你就不会出错。希望这可以帮助。

回答by Thomas Williams

Here is an idea you could try

这是一个你可以尝试的想法

In your main head do this

在你的主要头脑中这样做

<!DOCTYPE html> 
<html>
<head>
   <link rel="shortcut icon" href="<%=request.getContextPath()%>/images/favicon.ico" type="image/x-icon" />
   <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
   <script src="js/jquery.js"></script>
   <link rel=stylesheet type="text/css" href="dashboard.css" >

Notice I have left off the end head tag. Then in all your files you have to either close the head tag, or add extra header stuff and then close your head tag eg

注意我已经去掉了结束头标签。然后在所有文件中,您必须关闭 head 标签,或者添加额外的标题内容,然后关闭 head 标签,例如

      <title>Main page</title>
      <script src="main.js"></script>
   </head>
   <body>
      <jsp:include page="Header.jsp" flush="true" />
      .....
      other HTML contents specific to main page
      .....
   </body>
</html>

As for your page title you can run a little php to determine what page you are on

至于您的页面标题,您可以运行一些 php 来确定您所在的页面

回答by Sagar Awasthi

As per W3C standards, No! you can not have it.

根据 W3C 标准,不!你不能拥有它。

In your case, you are using JSP as a server side scripting. The problem can be solved by using CONSTANTS for stylesheet/scripts/other html elements.

在您的情况下,您使用 JSP 作为服务器端脚本。该问题可以通过对样式表/脚本/其他 html 元素使用 CONSTANTS 来解决。

You just need to add condition in your "main.jsp" file as per the page requirement.

您只需要根据页面要求在“main.jsp”文件中添加条件。

回答by Sagar Awasthi

As per W3C standards, you can not have two HEAD tags.

根据 W3C 标准,您不能有两个 HEAD 标签。

Concerning your issue, you can call header.jsp file without HEAD tag or may be you can rename to scripts.jsp or constants.jsp

关于您的问题,您可以调用没有 HEAD 标记的 header.jsp 文件,或者您可以重命名为 scripts.jsp 或 constants.jsp

For example:

例如:

Header.jsp

头文件.jsp

<link rel="shortcut icon" href="<%=request.getContextPath()%>/images/favicon.ico" type="image/x-icon" />
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<script src="js/jquery.js"></script>
<link rel=stylesheet type="text/css" href="dashboard.css" >

Main.jsp

主程序

<!DOCTYPE html>
<html>
<head> 
<title>Main page</title>
<script src="main.js"></script>
<jsp:include page="Header.jsp" flush="true" />
</head>
<body> 
..... 
other HTML contents specific to main page
..... 
</body>
</html>