如何将一个HTML文件包含在另一个中?

时间:2020-12-02 22:53:38

I have tried every possible question here on * but unable to resolve this issue ...

我已经在*上尝试了所有可能的问题,但无法解决此问题...

    <!--#include virtual="include.shtml"-->
    <!--#include virtual="include.html"-->
    <!--#include file="include.shtml"-->
    <!--#include file="include.html"-->
    <!--#include virtual="/include.shtml"-->
    <!--#include virtual="/include.html"-->
    <!--#include file="/include.shtml"-->
    <!--#include file="/include.html"-->
    <? include("/include.shtml"); ?>
    <? include("include.shtml"); ?>
    <? include("/include.html"); ?>
    <? include("include.html"); ?>

I tried with apache server running at localhost/include/index.html or file:///home/sahil/Desktop/include/index.html with all above includes but none of them is working for me :( .Now which method should i use to include one HTML file into another , considering my index.html and include.html both are in same directory ???

我尝试使用运行在localhost / include / index.html的apache服务器或者文件:///home/sahil/Desktop/include/index.html以及所有上述包含,但它们都不适合我:(。现在应该使用哪种方法我用来将一个HTML文件包含到另一个中,考虑到我的index.html和include.html都在同一个目录中???

4 个解决方案

#1


15  

The former syntax is SSI, the latter is PHP. Both are server technologies and will only work if accessed from an HTTP server that supports and is configured to check the file for the syntax (which usually means you have to turn the support on and use a .shtml/.php file extension (or change the config from the default to determining which files to check)). Other server side technologies are available.

前一种语法是SSI,后者是PHP。两者都是服务器技术,只有从支持并配置为检查文件的语法的HTTP服务器访问时才能工作(这通常意味着您必须打开支持并使用.shtml / .php文件扩展名(或更改)从默认配置到确定要检查的文件))。其他服务器端技术可用。

The only "include" mechanisms in HTML itself are (i)frames and objects.

HTML本身唯一的“包含”机制是(i)框架和对象。

You could also consider a template system such as TT that you could run as a build step to generate static HTML documents (NB: TT can also be used on the fly).

您还可以考虑使用TT等模板系统作为构建步骤来生成静态HTML文档(NB:TT也可以在运行中使用)。

#2


7  

HTML is Static

It is not possible to include a HTML page within another HTML page with static HTML because it is simply not supported by HTML. To make your HTML dynamic, you have 2 possibilities: use a client side script or use a server side technology.

使用静态HTML在另一个HTML页面中包含HTML页面是不可能的,因为HTML不支持HTML页面。要使HTML动态化,您有两种可能:使用客户端脚本或使用服务器端技术。

...Unless you start using frames (dropped with the HTML5 standard) or iframes that are most likely not a solution because of the fact that it is treated as a completely different web page.

...除非您开始使用框架(使用HTML5标准删除)或非常可能不是解决方案的iframe,因为它被视为完全不同的网页。

Client Solution

You could create a JavaScript file and write your HTML with the document.write function and then include the JavaScript file where ever you need it. Also, you could use some AJAX for this, but there are JavaScript libraries out there that could ease your burden such as the popular jQuery library.

您可以创建一个JavaScript文件并使用document.write函数编写HTML,然后将JavaScript文件包含在您需要的位置。此外,您可以使用一些AJAX,但有一些JavaScript库可以减轻您的负担,如流行的jQuery库。

Yet, I would not suggest using a client solution because it is more trouble than it is worth...

然而,我不建议使用客户端解决方案,因为它比它的价值更麻烦......

Server Solution

There are many server side solutions out there: ASP, ASP.NET, ASP.NET MVC, Java, PHP, Ruby and the list goes on. What you need to understand is that a server a side technology could be described as a parser for a specific server side language to automate certain tedious tasks and to perform actions that would represent a security risk on the client.

有很多服务器端解决方案:ASP,ASP.NET,ASP.NET MVC,Java,PHP,Ruby,这个列表还在继续。您需要了解的是,服务器端技术可以被描述为特定服务器端语言的解析器,以自动执行某些繁琐的任务并执行代表客户端安全风险的操作。

Of course you will need to have the means to host such a site and to configure the hosting environment. For example, you could host a PHP website with Apache and even have a developer hosting environment such as /MAMP/LAMP/WAMP.

当然,您需要拥有托管此类站点和配置托管环境的方法。例如,您可以使用Apache托管PHP网站,甚至可以拥有开发人员托管环境,例如/ MAMP / LAMP / WAMP。

You can view an example of including a page in PHP in the online documentation.

您可以在联机文档中查看在PHP中包含页面的示例。

Personally, I would be more inclined to use a server side solution.

就个人而言,我更倾向于使用服务器端解决方案。

#3


3  

HTML doesn't have an 'include' mechanism - I'm not sure where you've seen these solutions on *. You've probably been looking at answers for a server side language such as PHP or ASP.

HTML没有“包含”机制 - 我不确定您在*上看到过这些解决方案的位置。您可能一直在寻找服务器端语言(如PHP或ASP)的答案。

You do have the following options for 'pure' HTML where you only have static pages and JavaScript:

对于只有静态页面和JavaScript的“纯”HTML,您有以下选项:

  1. Frames
  2. 框架
  3. Ajax
  4. 阿贾克斯

See my answer here for sample code and more details (and also a caveat about SEO if that matters to you).

请参阅我的答案,以获取示例代码和更多详细信息(如果对您而言,还有关于SEO的警告)。

#4


-1  

Make your html file you want to include to php file. (only change the extension - to .php). If the files are in one directory, include code is:

将您要包含的html文件包含到php文件中。 (仅更改扩展名 - .php)。如果文件在一个目录中,则包含代码为:

<?php include "nameoffile.php" ?>

#1


15  

The former syntax is SSI, the latter is PHP. Both are server technologies and will only work if accessed from an HTTP server that supports and is configured to check the file for the syntax (which usually means you have to turn the support on and use a .shtml/.php file extension (or change the config from the default to determining which files to check)). Other server side technologies are available.

前一种语法是SSI,后者是PHP。两者都是服务器技术,只有从支持并配置为检查文件的语法的HTTP服务器访问时才能工作(这通常意味着您必须打开支持并使用.shtml / .php文件扩展名(或更改)从默认配置到确定要检查的文件))。其他服务器端技术可用。

The only "include" mechanisms in HTML itself are (i)frames and objects.

HTML本身唯一的“包含”机制是(i)框架和对象。

You could also consider a template system such as TT that you could run as a build step to generate static HTML documents (NB: TT can also be used on the fly).

您还可以考虑使用TT等模板系统作为构建步骤来生成静态HTML文档(NB:TT也可以在运行中使用)。

#2


7  

HTML is Static

It is not possible to include a HTML page within another HTML page with static HTML because it is simply not supported by HTML. To make your HTML dynamic, you have 2 possibilities: use a client side script or use a server side technology.

使用静态HTML在另一个HTML页面中包含HTML页面是不可能的,因为HTML不支持HTML页面。要使HTML动态化,您有两种可能:使用客户端脚本或使用服务器端技术。

...Unless you start using frames (dropped with the HTML5 standard) or iframes that are most likely not a solution because of the fact that it is treated as a completely different web page.

...除非您开始使用框架(使用HTML5标准删除)或非常可能不是解决方案的iframe,因为它被视为完全不同的网页。

Client Solution

You could create a JavaScript file and write your HTML with the document.write function and then include the JavaScript file where ever you need it. Also, you could use some AJAX for this, but there are JavaScript libraries out there that could ease your burden such as the popular jQuery library.

您可以创建一个JavaScript文件并使用document.write函数编写HTML,然后将JavaScript文件包含在您需要的位置。此外,您可以使用一些AJAX,但有一些JavaScript库可以减轻您的负担,如流行的jQuery库。

Yet, I would not suggest using a client solution because it is more trouble than it is worth...

然而,我不建议使用客户端解决方案,因为它比它的价值更麻烦......

Server Solution

There are many server side solutions out there: ASP, ASP.NET, ASP.NET MVC, Java, PHP, Ruby and the list goes on. What you need to understand is that a server a side technology could be described as a parser for a specific server side language to automate certain tedious tasks and to perform actions that would represent a security risk on the client.

有很多服务器端解决方案:ASP,ASP.NET,ASP.NET MVC,Java,PHP,Ruby,这个列表还在继续。您需要了解的是,服务器端技术可以被描述为特定服务器端语言的解析器,以自动执行某些繁琐的任务并执行代表客户端安全风险的操作。

Of course you will need to have the means to host such a site and to configure the hosting environment. For example, you could host a PHP website with Apache and even have a developer hosting environment such as /MAMP/LAMP/WAMP.

当然,您需要拥有托管此类站点和配置托管环境的方法。例如,您可以使用Apache托管PHP网站,甚至可以拥有开发人员托管环境,例如/ MAMP / LAMP / WAMP。

You can view an example of including a page in PHP in the online documentation.

您可以在联机文档中查看在PHP中包含页面的示例。

Personally, I would be more inclined to use a server side solution.

就个人而言,我更倾向于使用服务器端解决方案。

#3


3  

HTML doesn't have an 'include' mechanism - I'm not sure where you've seen these solutions on *. You've probably been looking at answers for a server side language such as PHP or ASP.

HTML没有“包含”机制 - 我不确定您在*上看到过这些解决方案的位置。您可能一直在寻找服务器端语言(如PHP或ASP)的答案。

You do have the following options for 'pure' HTML where you only have static pages and JavaScript:

对于只有静态页面和JavaScript的“纯”HTML,您有以下选项:

  1. Frames
  2. 框架
  3. Ajax
  4. 阿贾克斯

See my answer here for sample code and more details (and also a caveat about SEO if that matters to you).

请参阅我的答案,以获取示例代码和更多详细信息(如果对您而言,还有关于SEO的警告)。

#4


-1  

Make your html file you want to include to php file. (only change the extension - to .php). If the files are in one directory, include code is:

将您要包含的html文件包含到php文件中。 (仅更改扩展名 - .php)。如果文件在一个目录中,则包含代码为:

<?php include "nameoffile.php" ?>