8

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

Sahil Grover
  • 1,862
  • 4
  • 26
  • 57
  • 2
    The first kind of code you have is [SSI](http://bignosebird.com/ssi.shtml) (the grey one) and the second is [PHP](http://php.net/) (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](http://api.jquery.com/load/)) or an iframe. – Nathan Sep 25 '11 at 00:46

4 Answers4

15

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    How to do which of the multiple (mostly hyperlinked) things that I suggested? – Quentin Sep 25 '11 at 00:53
  • i tried with Iframes thats not a good thing to add. ell me with objects , I frames disturbs the whole page CSS by having extra margin and padding for it – Sahil Grover Sep 25 '11 at 00:55
  • Objects are effectively iframes with worse browser support when used to include HTML documents. So switching to from iframe to object only creates problems (except those involving beancounters demanding Strict variants of HTML when the problem demands something else for an optimal solution). – Quentin Sep 25 '11 at 00:58
  • is there no method to silently place a html file into other , no margin , no padding no border ?? – Sahil Grover Sep 25 '11 at 01:02
  • Not built into HTML. This is why people use template systems and includes. – Quentin Sep 25 '11 at 01:06
  • You pick a template or include system (I linked to three in the original answer) and then read the manual for it. – Quentin Sep 25 '11 at 07:03
8

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.

Alerty
  • 5,945
  • 7
  • 38
  • 62
3

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

Community
  • 1
  • 1
no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
  • can you tell me how to do through any one of them ?? – Sahil Grover Sep 25 '11 at 00:47
  • @SahilGrover I've just edited my answer to add a link to a more detailed answer I'd posted once before. See if that helps clarify things. – no.good.at.coding Sep 25 '11 at 00:48
  • I have seen your answers and iframe is not a good idea as it gives a lot of margin around it , and ajax is even bad in case broswer has javascript disabled . – Sahil Grover Sep 25 '11 at 00:54
  • @SahilGrover You should be able to style the `iframe` to remove all chrome, margins and scrollbars. And yes, you're right about JavaScript, I've mentioned that in my other answer as well. – no.good.at.coding Sep 25 '11 at 00:57
  • If neither of these works for you (and there's no reason to expect that these mechanisms will suit every need) then you might consider using PHP to include stuff without using it for any other server side scripting. You seem to be already on a server that supports this language (Apache httpd). – no.good.at.coding Sep 25 '11 at 01:00
-1

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" ?>
Jordan
  • 489
  • 2
  • 5
  • 9