如何将网站页眉和页脚存储在一个位置?
我总是重写我的页眉和页脚,对于每次编辑,我都必须手动将所有代码复制并粘贴到网页中。显然这是错误的方法,但我不确定正确的方法。我当前的想法是有一个“页眉”和“页脚”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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
听起来您需要一些服务器端技术。您选择哪一个(众多选项中)取决于您和您的托管。您的主机很可能支持 PHP 和 SSI(包括服务器端)。
在最简单的设置中,基本上将公共代码放入单独的文件中,例如
header.inc
和footer.inc
。这些文件的名称或扩展名是什么并不重要。对于 PHP,您的页面现在应该是
*.php
而不是*.html
,并且您需要编写以下代码:对于 SSI,您不必更改名称你的文件,你的代码将如下所示:
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
andfooter.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:For SSI, you don't have to change the names of your files, and your code would look like this:
你肯定不想用 Javascript 来做这件事。首先,页眉和页脚在 Javascript 执行之前不会加载,但更重要的是,任何未启用 Javascript 的人根本看不到它们(并且要求 Javascript 来查看静态页面没有意义)。
有两种简单的方法可以做到这一点:
1) 使用服务器端语言(如 php)来包含页脚:
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:
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.
通常,会使用一些服务器端模板技术,例如 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.
常见的方法是使用服务器端语言,例如 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
使用 HTML 或 PHP 包含。
Use HTML or PHP includes.