7
votes

I have tried every possible question here on stackoverflow 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 ???

4
The first kind of code you have is SSI (the grey one) and the second is PHP (the black and burgundy one). These are not for HTML pages. In HTML pages, you could try using AJAX (easiest way would be to use jQuery's .load) or an iframe.Nathan

4 Answers

15
votes

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.

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

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

7
votes

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.

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

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.

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.

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.

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

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

3
votes

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

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

  1. Frames
  2. Ajax

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

-1
votes

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:

<?php include "nameoffile.php" ?>