How to include an HTML page into another HTML page

2019-01-01 13:27发布

I want to include an HTML page inside an HTML page. Is it possible?

I don't want to do it in PHP, I know that in PHP, we can use include for this situation, how can I achieve the same purely in HTML without using the iframe and frame concept?

标签: html
14条回答
人间绝色
2楼-- · 2019-01-01 13:36

You can say that it is with PHP, but actually it has just one PHP command, all other files are just *.html.

  1. You should have a file named .htaccess in your web server's /public_html directory, or in another directory, where your html file will be and you will process one command inside it.
  2. If you do not have it, (or it might be hidden (you should check this directory list by checking directory for hidden files)), you can create it with notepad, and just type one row in it:
    AddType application/x-httpd-php .html
  3. Save this file with the name .htaccess and upload it to the server's main directory.
  4. In your html file anywhere you want an external "meniu.html file to appear, add te command: <?php include("meniu.html"); ?>.

That's all!

Remark: all commands like <? ...> will be treated as php executables, so if your html have question marks, then you could have some problems.

查看更多
与君花间醉酒
3楼-- · 2019-01-01 13:37

Also make sure to check out how to use Angular includes (using AngularJS). It's pretty straight forward…

<body ng-app="">
  <div ng-include="'myFile.htm'"></div>
</body> 
查看更多
怪性笑人.
4楼-- · 2019-01-01 13:41

If you're just trying to stick in your own HTML from another file, and you consider a Server Side Include to be "pure HTML" (because it kind of looks like an HTML comment and isn't using something "dirty" like PHP):

<!--#include virtual="/footer.html" -->
查看更多
琉璃瓶的回忆
5楼-- · 2019-01-01 13:41

If you are using NGINX over linux and want a pure bash/html, you can add a mask on your template and pipe the requests to use the sed command to do a replace by using a regullar expression.

Anyway I would rather have a bash script that takes from a templates folder and generate the final HTML.

查看更多
荒废的爱情
6楼-- · 2019-01-01 13:45

If you mean client side then you will have to use JavaScript or frames.
Simple way to start, try jQuery

$("#links").load("/Main_Page #jq-p-Getting-Started li");

More at jQuery Docs

If you want to use IFrames then start with Wikipedia on IFrames

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
        <title>Example</title>
  </head>
  <body>
        The material below comes from the website http://example.com/
        <iframe src="http://example.com/" height="200">
            Alternative text for browsers that do not understand IFrames.
        </iframe>
   </body>
</html>
查看更多
琉璃瓶的回忆
7楼-- · 2019-01-01 13:46

If you're willing to use jquery, there is a handy jquery plugin called "inc".

I use it often for website prototyping, where I just want to present the client with static HTML with no backend layer that can be quickly created/edited/improved/re-presented

http://johannburkard.de/blog/programming/javascript/inc-a-super-tiny-client-side-include-javascript-jquery-plugin.html

For example, things like the menu and footer need to be shown on every page, but you dont want to end up with a copy-and-paste-athon

You can include a page fragment as follows

<p class="inc:footer.htm"></p>
查看更多
登录 后发表回答