Equivalent of include() in HTML

34,483

Solution 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.

Solution 2

Have you tried:

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

Solution 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.

Solution 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

Solution 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

Share:
34,483

Related videos on Youtube

Trufa
Author by

Trufa

Existing code exerts a powerful influence. Its very presence argues that it is both correct and necessary.

Updated on July 09, 2022

Comments

  • Trufa
    Trufa almost 2 years

    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.

  • prodigitalson
    prodigitalson over 13 years
    well no, but it doesnt require something like PHP either.
  • Tyler Treat
    Tyler Treat over 13 years
    This is a server-side include, not plain HTML. Just as easy, if not easier, to use JavaScript probably.
  • prodigitalson
    prodigitalson over 13 years
    @tyler: except for then youre moving client side and generating a bunch of extra requests...
  • Your Common Sense
    Your Common Sense over 13 years
    but it does require something else! why not to use PHP then?
  • prodigitalson
    prodigitalson over 13 years
    @Col Shrapel: Well i would jsut use php as well, but the point is this is essentially built in to Apache and IIS so it doesnt require a 3rd party module being installed. That would be the only reason. But really, who DOESNT have PHP isntalled? It makes no sense to me either... but i didnt ask the question :-)
  • Tyler Treat
    Tyler Treat over 13 years
    @Col. Shrapnel: I was going under the assumption OP was trying to avoid a server-side solution for whatever reason. If I were in his position of course I'd use PHP.
  • Trufa
    Trufa over 13 years
    I am very limited, I am actually asking for fmbl tab pages in facebook.
  • Joe Dargie
    Joe Dargie almost 11 years
    One drawback is that users without JavaScript enabled won’t see content that’s written out like that. That currently includes search engine crawlers, so the content won’t show up in search engines.
  • Cavemanharris
    Cavemanharris almost 11 years
    Okay... thnx didnt think about that ! t
  • EML
    EML over 9 years
    Don't know why this has got so many downvotes. It's as good as most of the solutions here.
  • Darren Ringer
    Darren Ringer almost 9 years
    Very nice but has at least one pitfall I found: at least in chrome, clicking a link inside the object frame will by default open the new link INSIDE the object, rather than in the browser. Setting link targets to target="_top" in the included file is one possible workaround.
  • skywalker
    skywalker almost 8 years
    "but it does require something else" It requires apache which is required by PHP. PHP adds even more requirements.
  • Ayoub Laazazi
    Ayoub Laazazi over 5 years
    Another remark is that you need to call the JS and CSS files twice, in the main page and in the called html file
  • Brett Slocum
    Brett Slocum almost 4 years
    What if the HTML file is on another server within the firewall? In other words, a content server controlled by us.
  • Server Overflow
    Server Overflow about 3 years
    Thanks. Looks like the ultimate solution! BUT I get an error from Firefox, when loading the file locally (not on FTP): Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:////_topmenu2.html. (Reason: CORS request not http). ​