HTML有办法调用另一个文件吗?与php类似的包括


Is there a way with HTML to call to another file? Similar to php include?

是否有HTML非php方式调用文件?例如,我在一个大型网站上工作,当我想编辑菜单、页脚等时,我不想编辑每个页面。有点像wordpress如何使用php来包含标题/菜单数据,我希望有一个文件可以编辑,但不使用php。我不知道这有多可能。我正在用HTML5/Bootstrap/JS开发网站,如果可能的话,我想避免php!

可以使用的一种方法是.shtml文件扩展名。

它还具有SSI功能,并且是服务器端,而不是客户端,因此它在服务器上运行。

服务器端包含(SSI)

包含文件的基本语法是:

<!--#include virtual="/footer.html" -->
<!--#include file="footer.html" -->
<!--#include virtual="/cgi-bin/counter.pl" -->

您还可以在包含的文件中包含其他文件。

例如,假设您有一个头文件<!--#include virtual="/header.shtml" -->,并且您想要包含一个导航栏,那么您只需执行

header.shtml将包含<!--#include virtual="/nav.shtml" -->

都在同一个文件中,比如说你称之为index.shtml

index.shtml将包含这种结构

<!doctype html>
<head>
    <!--#include virtual="/js_files.shtml" -->
    <!--#include virtual="/metatags.shtml" -->
    <title>
    <!--#include virtual="/title.shtml" -->
    </title>
</head>
<body>
    <!--#include virtual="/header.shtml" -->
    <div>Hello world!</div>
    <!--#include virtual="/footer.shtml" -->
</body>
</html>

<!--#include virtual="/header.shtml" --> 内部

你可以把这个标签包括在<!--#include virtual="/nav.shtml" -->

包含

<a href="index.shtml">Home</a> - 
<a href="about.shtml">About</a> - 
<a href="contact.shtml">Info</a>
  • 通过对所有文件使用相同或相似的结构,您对一个(包括)文件所做的任何更改都将在整个网站上受到全局影响。

  • 这是我自己在某些网站上使用的一种方法,在这些网站上PHP不是必需的,我已经使用了很多年,效果很好,包括基于Bootstrap的。


参考文献:

  • http://httpd.apache.org/docs/2.2/howto/ssi.html
  • http://en.wikipedia.org/wiki/Server_Side_Includes
  • http://www.htmlgoodies.com/beyond/webmaster/article.php/3473341

如果您已经在使用java脚本,那么只需使用JQuery

使用jQuery:

index.html:

<html> 
  <head> 
    <script src="jquery.js"></script> 
    <script> 
    $(function(){
      $("#header").load("header.html"); 
      $("#footer").load("footer.html");
    });
    </script> 
  </head> 
  <body> 
     <div id="header"></div>
     <div id="footer"></div>
  </body> 
</html>

header.html

<p><b> This is my header file </b></p>

footer.html

<p><b> This is my footer file!</b></p>

您可以使用iframe将一个HTML页面包含在另一个页面中。