如何在不使用 php 的情况下制作模块化网站?
How to make a modular website without using php?
例如,使用 php 会将其添加到 index.php
:
<?php include("footer.php"); ?>
并在 footer.php
中添加 html 代码。
但是是否可以仅使用 html 而不使用 php 或 iframe 来做同样的事情?
我认为在纯 html 中,唯一的方法是使用 iframe,抱歉!
您可以尝试使用 localStorage:https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
The Web Storage API provides mechanisms by which browsers can store key/value pairs, in a much more intuitive fashion than using cookies.
这个想法的一个例子 (显然 SO 不允许在片段中这样做
可以从这里 tested/copied https://codepen.io/gc-nomade/pen/oGorNw )
window.onload = function() {
var loaded = localStorage.getItem("header");
if (loaded) {// if already set, load it from visitor
var header = localStorage.getItem("header");
var aside = localStorage.getItem("aside");
var footer = localStorage.getItem("footer");
document.getElementById("headers").innerHTML = header;
document.getElementById("aside").innerHTML = aside;
document.getElementById("footer").innerHTML = footer;
} else {// if first visit, load and store basic content once
var top = "<h1>hello</h1>";
var side = '<ul><li><a href="#">link</a>';
var bottom = "<p>My footer loaded via local storage </p>";
localStorage.setItem("header", top);
localStorage.setItem("aside", side);
localStorage.setItem("footer", bottom);
}
};
body {
margin: 0;
display: flex;
flex-flow: column;
height: 100vh;
background: crimson;
}
main {
flex: 1;
display: flex;
box-shadow: 2px;
}
aside {
width: 200px;
border: solid 2px;
background: tomato;
}
footer,
header {
background: lightblue;
box-shadow: 0 0 0 2px;
text-align: center;
}
aside img {
max-width: 50%;
display: block;
margin: 5vh auto;
}
<header id="headers"></header>
<main>
<aside id="aside"></aside>
<section> page content</section>
</main>
<footer id="footer"></footer>
例如,使用 php 会将其添加到 index.php
:
<?php include("footer.php"); ?>
并在 footer.php
中添加 html 代码。
但是是否可以仅使用 html 而不使用 php 或 iframe 来做同样的事情?
我认为在纯 html 中,唯一的方法是使用 iframe,抱歉!
您可以尝试使用 localStorage:https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
The Web Storage API provides mechanisms by which browsers can store key/value pairs, in a much more intuitive fashion than using cookies.
这个想法的一个例子 (显然 SO 不允许在片段中这样做 可以从这里 tested/copied https://codepen.io/gc-nomade/pen/oGorNw )
window.onload = function() {
var loaded = localStorage.getItem("header");
if (loaded) {// if already set, load it from visitor
var header = localStorage.getItem("header");
var aside = localStorage.getItem("aside");
var footer = localStorage.getItem("footer");
document.getElementById("headers").innerHTML = header;
document.getElementById("aside").innerHTML = aside;
document.getElementById("footer").innerHTML = footer;
} else {// if first visit, load and store basic content once
var top = "<h1>hello</h1>";
var side = '<ul><li><a href="#">link</a>';
var bottom = "<p>My footer loaded via local storage </p>";
localStorage.setItem("header", top);
localStorage.setItem("aside", side);
localStorage.setItem("footer", bottom);
}
};
body {
margin: 0;
display: flex;
flex-flow: column;
height: 100vh;
background: crimson;
}
main {
flex: 1;
display: flex;
box-shadow: 2px;
}
aside {
width: 200px;
border: solid 2px;
background: tomato;
}
footer,
header {
background: lightblue;
box-shadow: 0 0 0 2px;
text-align: center;
}
aside img {
max-width: 50%;
display: block;
margin: 5vh auto;
}
<header id="headers"></header>
<main>
<aside id="aside"></aside>
<section> page content</section>
</main>
<footer id="footer"></footer>