如何将网站页眉和页脚存储在一个位置?

发布于 2024-09-07 22:50:23 字数 182 浏览 4 评论 0原文

我总是重写我的页眉和页脚,对于每次编辑,我都必须手动将所有代码复制并粘贴到网页中。显然这是错误的方法,但我不确定正确的方法。我当前的想法是有一个“页眉”和“页脚”div,然后使用 jquery 的 $(document).ready 加载带有代码的 div。恐怕它会很慢,因为它在执行页眉/页脚代码之前等待整个页面渲染。处理这个问题的常用方法是什么?

I am always rewriting my headers and footers and for every edit i have to manually copy and paste all the code into the web pages. Obviously this is the wrong approach but im not sure of the right one. My current idea is to have a "header" and "footer" div and then with jquery's $(document).ready load the divs with code. Im afraid it will be slow though because it waits for the whole page to render before executing the header/footer code. What is a common way to handle this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

简单爱 2024-09-14 22:50:23

听起来您需要一些服务器端技术。您选择哪一个(众多选项中)取决于您和您的托管。您的主机很可能支持 PHP 和 SSI(包括服务器端)。

在最简单的设置中,基本上将公共代码放入单独的文件中,例如 header.incfooter.inc。这些文件的名称或扩展名是什么并不重要。

对于 PHP,您的页面现在应该是 *.php 而不是 *.html,并且您需要编写以下代码:

<?php include('header.inc'); ?>
<p>
    Here is your regular document.
</p>
<?php include('footer.inc'); ?>

对于 SSI,您不必更改名称你的文件,你的代码将如下所示:

<!--#include virtual="header.inc" -->
<p>
    Here is your regular document.
</p>
<!--#include virtual="footer.inc" -->

Sounds like you need some server-side technology. Which one (of the many options) you choose is up to you and your hosting. Most likely, your hosting will support PHP and SSI (Server-side includes).

In the simplest setup, basically put the common code into individual files, let's say header.inc and footer.inc. It doesn't matter what the name or extension of these files are.

For PHP, your pages should now be *.php instead of *.html and you need to write this code:

<?php include('header.inc'); ?>
<p>
    Here is your regular document.
</p>
<?php include('footer.inc'); ?>

For SSI, you don't have to change the names of your files, and your code would look like this:

<!--#include virtual="header.inc" -->
<p>
    Here is your regular document.
</p>
<!--#include virtual="footer.inc" -->
捎一片雪花 2024-09-14 22:50:23

你肯定不想用 Javascript 来做这件事。首先,页眉和页脚在 Javascript 执行之前不会加载,但更重要的是,任何未启用 Javascript 的人根本看不到它们(并且要求 Javascript 来查看静态页面没有意义)。

有两种简单的方法可以做到这一点:

1) 使用服务器端语言(如 php)来包含页脚:

<?php
  include('header.html');
?>
The rest of the page goes here
<?php
  include('footer.html');
?>

2) 使用生成静态页面的构建/部署过程。这与 1) 类似,但每次构建只会执行一次,而不是每次有人点击页面时执行一次。

You definitely don't want to do this with Javascript. First off, the header and footer won't load until the Javascript executes, but more important, anyone without Javascript enabled won't see them at all (and requiring Javascript to view a static page doesn't make sense).

There are two easy methods to do this:

1) Use a server-side language like php to include the footers:

<?php
  include('header.html');
?>
The rest of the page goes here
<?php
  include('footer.html');
?>

2) Use a build/deploy process that generates static pages. This would be similar to 1) but it would only be done once per build, rather than every time someone hits the page.

没有你我更好 2024-09-14 22:50:23

通常,会使用一些服务器端模板技术,例如 PHP、JSP 或 XSL。使用其中一种方法创建可链接的可重用代码段相当简单。

对于纯 HTML + JS 方法,您可以使用 IFRAME 链接到独立的页眉和页脚 HTML 文件。这将允许您将所有页眉和页脚信息保存在一个位置,只需要更新一次。

Commonly, there's some server-side templating technology in use, such as PHP, JSP or XSL. Creating reusable pieces of code which can be linked is fairly simple using one of these approaches.

For a pure HTML + JS approach, you could use an IFRAME to link to a standalone header and footer HTML file. That would allow you to keep all the header and footer information in one place, only needing to update it once.

瞳孔里扚悲伤 2024-09-14 22:50:23

常见的方法是使用服务器端语言,例如 PHP。或者,如果您只需要包含页脚和页眉,则可以使用 SSI

Common way is to use server side language like PHP. Or if you need only footer and header include you can use SSI

小草泠泠 2024-09-14 22:50:23

使用 HTML 或 PHP 包含。

Use HTML or PHP includes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文