Equivalent of include() in HTML

僤鯓⒐⒋嵵緔 提交于 2019-11-26 12:27:47

问题


I was wondering wether there is a way to include some html content inside another html using only html?

A replacement to PHP\'s

<?php include(\"file.php\"); ?>

Is this possible?

EDIT:

This has brought up some confusion, what I needed was \"almost an html tag\" that had the functionality of including a html document in another.


回答1:


It cannot be done purely by HTML. (There are iframes, however, but I don't think that qualifies in this case.)

It can be done using JavaScript. You get the other file via Ajax, and place its contents inside an HTML element on the current page.




回答2:


Have you tried:

<object type="text/html" data="file.html"></object>



回答3:


Shameless plug of a library that I wrote the solve this.

https://github.com/LexmarkWeb/csi.js

<div data-include="/path/to/include.html"></div>

The above will take the contents of /path/to/include.html and replace the div with it.




回答4:


HTML does not have a feature to include additional content natively. However most web servers do have server-side include statements:
SSI in Apache
SSI in IIS




回答5:


the only thing would be an iframe which is pure html. but you also can use javascript to get the page via ajax and include it into your dom hirarchy




回答6:


There's no such thing. You'd have to use a server-side scripting language or JavaScript to do something like this.




回答7:


Yes there is but you need to enable it in your config or .htaccess:

Options +Includes
AddType text/html .shtml
AddHandler server-parsed .shtml

Of course with that youd need to rename any file doing the including to .shtml... or you could jsut use:

Options +Includes
AddType text/html .html
AddHandler server-parsed .html

the syntax itself is similar to comment:

<!--#include virtual="/footer.html" -->



回答8:


If you're using Apache, you can try Server Side Includes.




回答9:


This might be a few years late but this is how i did it !

in the first line after put this line !

<SCRIPT LANGUAGE="JavaScript" src="http://yourdomain.com/header.js">

then create a file called "header.js" and enter the content of the file you want to include ! like so....

<!-- Begin
document.write('<center>');
document.write('<a href="http://alinktosomewhere.co.za">a link 1</a>');
document.write('<a href="http://alinktosomewhere.co.za">a link 1</a>');
document.write('<a href="http://alinktosomewhere.co.za">a link 1</a>');
document.write('<a href="http://alinktosomewhere.co.za">a link 1</a>');
document.write('<hr>');
document.write('</center>');
// End -->

Hope this help !




回答10:


The lack of Include\Import in Html is really frustrating!

A good alternative is "Server Side Includes (SSI)" in case "PHP" is not supported!

SSI is supported by almost any (if not all) web host server!

<!--#include virtual="layout.html" -->

The file that contains the above line must end with ".shtml" or ".shtm" extensions!


It's really annoying that something as easy as Include\Import can't be performed by the browser itself!

Like php or Node.js, preprocessing the html using Javascript itself before the HTML Load process start should be supported in any browser!



来源:https://stackoverflow.com/questions/3928331/equivalent-of-include-in-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!