Can HTML contain two HEAD tags

后端 未结 6 1946
盖世英雄少女心
盖世英雄少女心 2021-01-31 18:57

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 p

相关标签:
6条回答
  • 2021-01-31 19:12

    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

          <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

    0 讨论(0)
  • 2021-01-31 19:13

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

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

    For example:

    Header.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>
    
    0 讨论(0)
  • 2021-01-31 19:22

    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 know what you mean, they will take both head tags and work as expected.

    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.

    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.

    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:

    <!-- 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 more with 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 not broken syntax, it's not a 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.

    TLDR: In practice, two head tags work.

    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 head tag 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.

    0 讨论(0)
  • 2021-01-31 19:25

    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.

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

    0 讨论(0)
  • 2021-01-31 19:30

    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.

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

    0 讨论(0)
  • 2021-01-31 19:35

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

    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.

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

    0 讨论(0)
提交回复
热议问题