PDA

View Full Version : How to import one HTML file to another HTML file


smartks
11-15-2007, 06:18 AM
I have to develop one website using HTML. This website will contain 40 pages approximately. Each page contains sub menus, i need to edit sub menu once that will affects all pages. Is this possible?

gnomeontherun
11-15-2007, 06:56 AM
There are a couple options. If you have PHP abilities, you can use PHP includes which will just add the contents of a file to another file. You would have to make your files php files, which technically means only changing the file extension.
http://www.tizag.com/phpT/include.php

If that scares you, then you could also do a server side include which looks like this. You might have a problem with this if your server doesn't support this function (Apache server)
<!--#include file="filetoinclude.html" -->

http://http-server.carleton.ca/~dmcfet/html/ssi.html#include

Actinia
11-15-2007, 04:54 PM
The PHP route is probably the cleanest, but you could also use templates in Dreamweaver. These are a bit fiddly to set up, but once working, will do just what you want. If you edit the template, then Dreamweaver makes these changes in all files that use that template. Dreamweaver also allows you to make a global search & replace across all files in your site. I have found this a very useful facility.

I should add that some free HTML editors, such as NVU, also offer templates, but I don't know about the global search & replace

John Rostron

Actinia
11-15-2007, 07:14 PM
I should mention that server-side includes are another way.

Another route is to use Ajax. I have written most of my web pages for a Virtual Learning Environment (VLE) that was perl-based (so no PHP). It did not permit server-side includes either, but did require Javascript. I had two blocks of code replicated on all pages, one at the top and one at the bottom. I created two divs with suitable names, and on loading, these were each populated with html text from an include file.

John Rostron

trigger_tre
11-15-2007, 07:30 PM
Go the php route. Its super easy and helps eliminate excess code.

Also going the php route will open the door to numerous other snippets useful to many websites.

I use this method for my header, footer, navigation, and sidemenus.

-trigger

gnomeontherun
11-15-2007, 10:14 PM
Another route is to use Ajax.

I don't think importing HTML with AJAX is a practical use of AJAX. Yes it would work, but its not really used to just include a page within a page. It is used to make Rich Internet Applications, that have the functionality of some desktop applications. This also means that a page has to be loaded, and then the browser (assuming Javascript is on) has to reload a second page for longer loading and potential issues with browsers. So I think for a simple file include, PHP or SSI (Apache only) are your best options.

ASP also has the ability to include files, but that all depends on your server environment, we just usually assume PHP is available since its free :)

jonbey
11-16-2007, 12:07 AM
You can use php includes on html pages if you can configure the Apache conf files to allow htaccess to overide the rules. I use php includes on my site, and there are lots of them - menus, adverts, logos, footers, codes etc. (see my other thread for an example - http://www.codingforums.com/showthread.php?t=127920 - but remember, there are includes in includes, sometimes in includes!)

On the server you need to be able to alter this file (may vary for diff. Apache versions):

/etc/apache2/sites-available/default:


Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/


You need to change "AllowOverride None" to "AllowOverride All"

This will allow you to add the following code into any .htaccess file, to allow php to be parsed inside .html files. This means no changes to file names, URLS stay in tact, Search Engines are happy, no redirects required etc. etc.

In the .htaccess, add this:



RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html


Once done, create place your menu code into a file called menu.inc, and then whereve you would normally have your menu appear, type:



<?php include('menu.inc'); ?>


Then when you need to alter your menu over all 100 pages, you just change one file and upload. Easy. Tutorial source: http://www.webologist.co.uk/2007/09/rock-and-roll.html

For my site I have a header.inc file, and inside that is the
<?php include('menu.inc'); ?> code.

Actinia
11-16-2007, 03:22 PM
I would agree that Ajax is not an optimal solution generally, but it was the only one that was workable in these circumstances.

John Rostron

smartks
11-19-2007, 04:34 AM
Currently i am using asp.net .. In this can i include html file?

Apostropartheid
11-19-2007, 04:12 PM
I'm not an expert on ASP.NET, but Microsoft says:
<%
Response.WriteFile ("Yourfile.inc")
%>

smartks
11-23-2007, 04:06 AM
i don't know about ".inc" format can u explain

warran
11-23-2007, 05:36 AM
inc.html

document.write('<ul><li>1</li><li>2</li><li>3</li><li>4</li></ul>')


test.html

<HTML>
<BODY>
<script src="inc.html"></script>
</BODY>
</HTML>

DakotaChick
11-23-2007, 08:33 AM
i don't know about ".inc" format can u explain
Just think of it like a .txt or any other file type your familiar with. It's basicly just a file of text (html markup or plain text or script code, etc...) tagged with the extension inc to mark it as a file that is "inc"luded into another file.

Or at least thats how it was explained to me =)

Apostropartheid
11-23-2007, 04:10 PM
Yeah, basically, rather like the .ssi file extension for basic server-side includes.

smartks
11-28-2007, 10:11 AM
Thanks i try to use this in my own example