tags:

views:

99

answers:

6

Hi ,
Can HTML code be attached to HTML page like CSS file ?
So that if part of HTML code is replicated in all pages , I can put it in one file and then easly modify it .

Thanks,

Ahmed.

+1  A: 

You may want to have a look at SSI (Server Side Includes):

Server Side Includes (SSI) is a simple interpreted server-side scripting language used almost exclusively for the web. The most frequent use of SSI is to include the contents of one or more files into a web page on a web server.

Also, consider:

However, while HTML allows the direct inclusion of CSS, JavaScript and Image files into a web page it has never allowed direct inclusion of HTML. There are several tricks to achieve this, each with their own problems. These include:

  • Using an IFrame, which includes content in a clearly separate, fixed size area.
  • Converting the HTML code into a JavaScript program that inserts the HTML into the DOM.
  • Using JavaScript with Ajax to load the Html. Internet Explorer will generally not allow this to work directly from the file system due to security concerns.
Gregory Pakosz
or as suggested by Pekka, use PHP
Gregory Pakosz
A: 

I don't think it's possible with just HTML, you might be able to get away with it if you use a bit of JavaScript... of course if JavaScript is disabled your site wouldn't work.

That said have you considered using a scripting language such as PHP? You could include your html page in whenever you desire, you then only have to modify / maintain one file.

ILMV
+1  A: 

Not possible with pure html, if you are using a server-side language however, usually there is such function available, for example in PHP, there is include or require functions to include files.

PHP Example:

include ('file.html');

I would recommend you to go with the first (server-side) approach if you are using a server-side language.

Sarfraz
Note that using js for content is typically a very bad approach for a basic website--you have to be comfortable with it potentially not showing up, for your user or search engines...
D_N
also note the intent of `require` is about 'I want that functionality', while `include` really serves the 'that content here' purpose.
xtofl
@D_N: hmm i removed that part anyways.
Sarfraz
@xtofl: I have just given an example, i do not urge OP to use the require, he is supposed to see the documentation, i have provided the links for both and secondly, in my example the precedence is given to `include`. Thanks :)
Sarfraz
A: 

Maybe this link will help http://ask-leo.com/how_do_i_include_one_html_file_inside_another.html

Midhat
`Another popular ASP-like programming environment is PHP.` - Leo, you funny man.
ANeves
+3  A: 

Not in plain HTML, no. (Sadly)

As Gregory says, you could look into Server Side Includes - they are parsed by the Web Server.

Alternatively, if you have PHP, you can do an include():

<?php include "header.html"; ?>
Pekka
+4  A: 

Yes it can, using frames. Not recommended though.

Example:


<frameset cols="10%,90%">
   <frame src="navigation.html">
   <frame src="content.html">
</frameset> 

This code would allow you to reuse the content of file navigation.html. All links clicked on page content.html would change the content of that frame, and the navigation would stay as it is.

Using frames has many disadvantages (search engine problems, usability, linking, scrolling etc.) and shouldn't be considered in modern web sites (except on very rare occasions).

simon
@simon: yup it was important to say not recommended.
Sarfraz