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

前端 未结 11 1912
無奈伤痛
無奈伤痛 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:27

    I tried this: Create a file header.html like

    <!-- Meta -->
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <!-- JS -->
    <script type="text/javascript" src="js/lib/jquery-1.11.1.min.js" ></script>
    <script type="text/javascript" src="js/lib/angular.min.js"></script>
    <script type="text/javascript" src="js/lib/angular-resource.min.js"></script>
    <script type="text/javascript" src="js/lib/angular-route.min.js"></script>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    
    <title>Your application</title>
    

    Now include header.html in your HTML pages like:

    <head>
       <script type="text/javascript" src="js/lib/jquery-1.11.1.min.js" ></script>
       <script> 
         $(function(){ $("head").load("header.html") });
       </script>
    </head>
    

    Works perfectly fine.

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

    You could also put: (load_essentials.js:)

    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:33

    I've been working in C#/Razor and since I don't have IIS setup on my home laptop I looked for a javascript solution to load in views while creating static markup for our project.

    I stumbled upon a website explaining methods of "ditching jquery," it demonstrates a method on the site does exactly what you're after in plain Jane javascript (reference link at the bottom of post). Be sure to investigate any security vulnerabilities and compatibility issues if you intend to use this in production. I am not, so I never looked into it myself.

    JS Function

    var getURL = function (url, success, error) {
        if (!window.XMLHttpRequest) return;
        var request = new XMLHttpRequest();
        request.onreadystatechange = function () {
            if (request.readyState === 4) {
                if (request.status !== 200) {
                    if (error && typeof error === 'function') {
                        error(request.responseText, request);
                    }
                    return;
                }
                if (success && typeof success === 'function') {
                    success(request.responseText, request);
                }
            }
        };
        request.open('GET', url);
        request.send();
    };
    

    Get the content

    getURL(
        '/views/header.html',
        function (data) {
            var el = document.createElement(el);
            el.innerHTML = data;
            var fetch = el.querySelector('#new-header');
            var embed = document.querySelector('#header');
            if (!fetch || !embed) return;
            embed.innerHTML = fetch.innerHTML;
    
        }
    );
    

    index.html

    <!-- This element will be replaced with #new-header -->
    <div id="header"></div>
    

    views/header.html

    <!-- This element will replace #header -->
    <header id="new-header"></header>
    

    The source is not my own, I'm merely referencing it as it's a good vanilla javascript solution to the OP. Original code lives here: http://gomakethings.com/ditching-jquery#get-html-from-another-page

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

    Aloha from 2018. Unfortunately, I don't have anything cool or futuristic to share with you.

    I did however want to point out to those who have commented that the jQuery load() method isn't working in the present are probably trying to use the method with local files without running a local web server. Doing so will throw the above mentioned "cross origin" error, which specifies that cross origin requests such as that made by the load method are only supported for protocol schemes like http, data, or https. (I'm assuming that you're not making an actual cross-origin request, i.e the header.html file is actually on the same domain as the page you're requesting it from)

    So, if the accepted answer above isn't working for you, please make sure you're running a web server. The quickest and simplest way to do that if you're in a rush (and using a Mac, which has Python pre-installed) would be to spin up a simple Python http server. You can see how easy it is to do that here.

    I hope this helps!

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

    Must you use html file structure with JavaScript? Have you considered using PHP instead so that you can use simple PHP include object?

    If you convert the file names of your .html pages to .php - then at the top of each of your .php pages you can use one line of code to include the content from your header.php

    <?php include('header.php'); ?>
    

    Do the same in the footer of each page to include the content from your footer.php file

    <?php include('footer.php'); ?>
    

    No JavaScript / Jquery or additional included files required.

    NB You could also convert your .html files to .php files using the following in your .htaccess file

    # re-write html to php
    RewriteRule ^(.*)\.html$ $1.php [L]
    RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
    
    
    # re-write no extension to .php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    
    0 讨论(0)
  • 2020-11-22 07:37

    I add common parts as header and footer using Server Side Includes. No HTML and no JavaScript is needed. Instead, the webserver automatically adds the included code before doing anything else.

    Just add the following line where you want to include your file:

    <!--#include file="include_head.html" -->
    
    0 讨论(0)
提交回复
热议问题