9

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

1
  • 2
    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. Commented Sep 25, 2011 at 0:46

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

6 Comments

How to do which of the multiple (mostly hyperlinked) things that I suggested?
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
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).
is there no method to silently place a html file into other , no margin , no padding no border ??
Not built into HTML. This is why people use template systems and includes.
|
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.

Comments

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

5 Comments

can you tell me how to do through any one of them ??
@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.
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 .
@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.
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).
-2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.