Make header and footer files to be included in multiple html pages

前端 未结 11 1925
無奈伤痛
無奈伤痛 2020-11-22 06:49

I want to create common header and footer pages that are included on several html pages.

I\'d like to use javascript. Is there a way to do this using only html and

相关标签:
11条回答
  • 2020-11-22 07:38

    It is also possible to load scripts and links into the header. I'll be adding it one of the examples above...

    <!--load_essentials.js-->
    document.write('<link rel="stylesheet" type="text/css" href="css/style.css" />');
    document.write('<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />');
    document.write('<script src="js/jquery.js" type="text/javascript"></script>');
    
    document.getElementById("myHead").innerHTML =
    "<span id='headerText'>Title</span>"
    + "<span id='headerSubtext'>Subtitle</span>";
    document.getElementById("myNav").innerHTML =
    "<ul id='navLinks'>"
    + "<li><a href='index.html'>Home</a></li>"
    + "<li><a href='about.html'>About</a>"
    + "<li><a href='donate.html'>Donate</a></li>"
    + "</ul>";
    document.getElementById("myFooter").innerHTML =
    "<p id='copyright'>Copyright &copy; " + new Date().getFullYear() + " You. All"
    + " rights reserved.</p>"
    + "<p id='credits'>Layout by You</p>"
    + "<p id='contact'><a href='mailto:you@you.com'>Contact Us</a> / "
    + "<a href='mailto:you@you.com'>Report a problem.</a></p>";
    
    <!--HTML-->
    <header id="myHead"></header>
    <nav id="myNav"></nav>
    Content
    <footer id="myFooter"></footer>
    
    <script src="load_essentials.js"></script>
    
    0 讨论(0)
  • 2020-11-22 07:39

    I think, answers to this question are too old... currently some desktop and mobile browsers support HTML Templates for doing this.

    I've built a little example:

    Tested OK in Chrome 61.0, Opera 48.0, Opera Neon 1.0, Android Browser 6.0, Chrome Mobile 61.0 and Adblocker Browser 54.0
    Tested KO in Safari 10.1, Firefox 56.0, Edge 38.14 and IE 11

    More compatibility info in canisue.com

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>HTML Template Example</title>
    
        <link rel="stylesheet" href="styles.css">
        <link rel="import" href="autoload-template.html">
    </head>
    <body>
    
    <div class="template-container">1</div>
    <div class="template-container">2</div>
    <div class="template-container">3</div>
    <div class="template-container">4</div>
    <div class="template-container">5</div>
    
    </body>
    </html>
    

    autoload-template.html

    <span id="template-content">
        Template Hello World!
    </span>
    
    <script>
        var me = document.currentScript.ownerDocument;
        var post = me.querySelector( '#template-content' );
    
        var container = document.querySelectorAll( '.template-container' );
    
        //alert( container.length );
        for(i=0; i<container.length ; i++) {
            container[i].appendChild( post.cloneNode( true ) );
        }
    </script>
    

    styles.css

    #template-content {
        color: red;
    }
    
    .template-container {
        background-color: yellow;
        color: blue;
    }
    

    Your can get more examples in this HTML5 Rocks post

    0 讨论(0)
  • 2020-11-22 07:46

    Save the HTML you want to include in an .html file:

    Content.html

    <a href="howto_google_maps.asp">Google Maps</a><br>
    <a href="howto_css_animate_buttons.asp">Animated Buttons</a><br>
    <a href="howto_css_modals.asp">Modal Boxes</a><br>
    <a href="howto_js_animate.asp">Animations</a><br>
    <a href="howto_js_progressbar.asp">Progress Bars</a><br>
    <a href="howto_css_dropdown.asp">Hover Dropdowns</a><br>
    <a href="howto_js_dropdown.asp">Click Dropdowns</a><br>
    <a href="howto_css_table_responsive.asp">Responsive Tables</a><br>
    

    Include the HTML

    Including HTML is done by using a w3-include-html attribute:

    Example

        <div w3-include-html="content.html"></div>
    

    Add the JavaScript

    HTML includes are done by JavaScript.

        <script>
        function includeHTML() {
          var z, i, elmnt, file, xhttp;
          /*loop through a collection of all HTML elements:*/
          z = document.getElementsByTagName("*");
          for (i = 0; i < z.length; i++) {
            elmnt = z[i];
            /*search for elements with a certain atrribute:*/
            file = elmnt.getAttribute("w3-include-html");
            if (file) {
              /*make an HTTP request using the attribute value as the file name:*/
              xhttp = new XMLHttpRequest();
              xhttp.onreadystatechange = function() {
                if (this.readyState == 4) {
                  if (this.status == 200) {elmnt.innerHTML = this.responseText;}
                  if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
                  /*remove the attribute, and call this function once more:*/
                  elmnt.removeAttribute("w3-include-html");
                  includeHTML();
                }
              } 
              xhttp.open("GET", file, true);
              xhttp.send();
              /*exit the function:*/
              return;
            }
          }
        }
        </script>
    

    Call includeHTML() at the bottom of the page:

    Example

    <!DOCTYPE html>
    <html>
    <script>
    function includeHTML() {
      var z, i, elmnt, file, xhttp;
      /*loop through a collection of all HTML elements:*/
      z = document.getElementsByTagName("*");
      for (i = 0; i < z.length; i++) {
        elmnt = z[i];
        /*search for elements with a certain atrribute:*/
        file = elmnt.getAttribute("w3-include-html");
        if (file) {
          /*make an HTTP request using the attribute value as the file name:*/
          xhttp = new XMLHttpRequest();
          xhttp.onreadystatechange = function() {
            if (this.readyState == 4) {
              if (this.status == 200) {elmnt.innerHTML = this.responseText;}
              if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
              /*remove the attribute, and call this function once more:*/
              elmnt.removeAttribute("w3-include-html");
              includeHTML();
            }
          }      
          xhttp.open("GET", file, true);
          xhttp.send();
          /*exit the function:*/
          return;
        }
      }
    };
    </script>
    <body>
    
    <div w3-include-html="h1.html"></div> 
    <div w3-include-html="content.html"></div> 
    
    <script>
    includeHTML();
    </script>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 07:48

    You can accomplish this with jquery.

    Place this code in index.html

    <html>
    <head>
    <title></title>
    <script
        src="https://code.jquery.com/jquery-3.3.1.js"
        integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
        crossorigin="anonymous">
    </script>
    <script> 
    $(function(){
      $("#header").load("header.html"); 
      $("#footer").load("footer.html"); 
    });
    </script> 
    </head>
    <body>
    <div id="header"></div>
    <!--Remaining section-->
    <div id="footer"></div>
    </body>
    </html>
    

    and put this code in header.html and footer.html, at the same location as index.html

    <a href="http://www.google.com">click here for google</a>
    

    Now, when you visit index.html, you should be able to click the link tags.

    0 讨论(0)
  • 2020-11-22 07:54

    another approach made available since this question was first asked is to use reactrb-express (see http://reactrb.org) This will let you script in ruby on the client side, replacing your html code with react components written in ruby.

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