Equivalent of include() in HTML

后端 未结 11 476
有刺的猬
有刺的猬 2020-11-29 06:40

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

A replacement to PHP\'s



        
相关标签:
11条回答
  • 2020-11-29 07:15

    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

    0 讨论(0)
  • 2020-11-29 07:15

    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!

    0 讨论(0)
  • 2020-11-29 07:19

    Have you tried:

    <object type="text/html" data="file.html"></object>
    
    0 讨论(0)
  • 2020-11-29 07:22

    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" -->
    
    0 讨论(0)
  • 2020-11-29 07:23

    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.

    0 讨论(0)
提交回复
热议问题