我是靠谱客的博主 迷路万宝路,最近开发中收集的这篇文章主要介绍在html页面中包含共享页面How do I include one HTML file inside another?,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

ask leo!

How do I include one HTML file inside another?

Home » Web » Web Site Management

Summary: Using "include files" you can more easily organize data and remove the need to repeat the same information in several different files on your web site.

<script style="display: none;" language="JavaScript" type="text/javascript" src="http://ads.pugetsoundsoftware.com/adx.js"></script> <script language="JavaScript" type="text/javascript"> </script><script style="display: none;" language="JavaScript" type="text/javascript" src="http://ads.pugetsoundsoftware.com/adjs.php?n=169094125&what=adblock1,_entryid149%7Cadblock1,_web%7Cadblock1,_web_site_management%7Cadblock1def&exclude=,&referer=http%3A//www.google.cn/search%3Fhl%3Dzh-CN%26newwindow%3D1%26q%3Dhtml+include+file%26btnG%3DGoogle+%25E6%2590%259C%25E7%25B4%25A2%26meta%3D%26aq%3D1%26oq%3Dhtml+include"></script>

How do I include one HTML file inside another?

It's very common practice to have a consistent theme on a web site. You might have a standard navigation bar or a logo or even just a page footer with copyright and administrative information. Rather than actually having that information on each and every page it would certainly be nice if you could write your navigation bar once, keep it in one file, and then reference that file in each of several different pages. Make a change to the navigation bar in one place and instantly all pages are updated.

Welcome to "include" files - an incredibly powerful facility that can do this, and so much more, on your web site.

Includes break down into two categories: client and server. A "client" side include is one performed by your browser. Unfortunately, there is no specific syntax in HTML for client side includes so we have to play a small game using javascript. A "server" side include is exactly that - the include happens on your web server so the client browser never even knows it happened.

Server Side Includes

We'll start with the conceptually easier one: the server side include. The specific syntax will vary based on what type of server you have and what your pages are written in.

Simple HTML pages on most common web servers can use a syntax called Server Side Include, or SSI. As an example in an HTML file a.html we can place this line:

<!--#include FILE="b.inc" -->

The page seen by a browser viewing a.html will consist of the contents of a.html before the include line, followed by the contents of b.inc, followed by the contents of a.html after the include line. Put the HTML for your navigation bar in a file like b.inc, and all your pages can show the exact same bar.

SSI is available on both Apache and Microsoft IIS web servers. On Apache some configuration may be needed but even if you don't have access to the actual server configuration files it can typically also be enabled by commands in a file named .htaccess that you will either find or can create in your server's web directory. Read more about Apache SSI here. Under IIS, SSI is enabled anytime you use ".asp" pages -- so the only configuration you need do is to name your pages .asp instead of .html. Read more about Server Side Include in ASP pages here.

Another popular ASP-like programming environment is PHP. PHP's include syntax is very simple:

<? readfile("b.inc"); ?>

Naturally, PHP has a host of additional processing ability but much like ASP the only requirement to make the include above work is to have PHP on your web server and name your file ".php".

With all of the above approaches, the browser viewing the page knows absolutely nothing about the include - it all happened before the page was downloaded. However, sometimes processing an include on the server isn't the right option. That's where processing an include on the client comes in.

Client Side Includes

As I mentioned above, there is no actual syntax for a client-side include but we can mimic one using Javascript. For example:

<script src="b.js" type="text/javascript"> </script>

When encountered the browser downloads the script "b.js", executes it, and prints any output that the script might generate as if it were inline HTML. Technically that's not an include but the script "b.js" could be nothing more than a series of javascript "print" statements such as these:

document.write("<table>")
document.write("<tr>")
... and so on

You can see it's "printing" the HTML you want included. In other words, if you can format your include file as a series of javascript prints, you can use client-side include to insert it.

Now things can get very interesting, because we'll introduce two things: remote includes, and CGI programs, into the mix.

Remote Includes

The files we've included so far have been assumed to be on your own server in the same location as your other HTML pages. In almost all cases you can "include" using a full URL to any other server on the internet.

For client-side includes it's very simple. It just works:

<script src="http://example.com/b.js" type="text/javascript"> </script>

This works just like the earlier example execpt that b.js will get loaded from example.com rather than your own server. Similarly, PHP also "just works":

<? readfile("http://example.com/b.inc"); ?>

Unfortunately Apache SSI directives do not support remote includes. But there's almost always a way and we have a workaround using CGI.

<script style="display: none;" language="JavaScript" type="text/javascript" src="http://ads.pugetsoundsoftware.com/adx.js"></script> <script language="JavaScript" type="text/javascript"> </script><script style="display: none;" language="JavaScript" type="text/javascript" src="http://ads.pugetsoundsoftware.com/adjs.php?n=947210679&what=adblock2def&exclude=,&referer=http%3A//www.google.cn/search%3Fhl%3Dzh-CN%26newwindow%3D1%26q%3Dhtml+include+file%26btnG%3DGoogle+%25E6%2590%259C%25E7%25B4%25A2%26meta%3D%26aq%3D1%26oq%3Dhtml+include"></script>

CGI Includes

So far we've included only "static" HTML pages. As it turns out you can "include" the output of a server-run CGI program. This then becomes our solution for Apache's lack of support for remote includes. We'll start with this very short Perl program:

use LWP::Simple;
print "Content-type:text/html/n/n";
getprint ($ENV{'QUERY_STRING'});

We'll call it "proxy.pl". Proxy.pl must be appropriately installed on your server into your cgi-bin directory or its equivalent. By being local to your server Include directives can reference it.

Proxy.pl simply fetches the contents of URL passed as a parameter. This means we can perform an apache remote include this way:

<!--#include VIRTUAL="/cgi-bin/proxy.pl?http://example.com/b.inc" -->

It works like this:

  • The include executes proxy.pl with the parameter "http://example.com/b.inc"
  • Proxy.pl then fetches b.inc from example.com and prints it.

The result is that the contents of b.inc show up as an included file.

Includes + remote includes + CGI

So far we've used a CGI program to fetch what is essentially just another static html file. In fact, if we put all these pieces together we can create some very useful and interesting internet applications.

Randy Cassingham of This is True wanted to be able to provide his readers who had web sites the ability to host one of his stories. Sounds like a job for an include file, right? But he also wanted that story to change every day. That's more than a static HTML page; that's a CGI program that outputs a different story each day.

The result is tad.pl (True-A-Day) - a Perl script that picks a new story every day and outputs HTML. Given what we've talked about so far so you can probably guess how it's used. As a client side include:

<script src="http://www.thisistrue.net/cgi-bin/tad.pl" type="text/javascript"> </script>

That's it in its simplest form. Note that:

  • tad.pl is written in Perl. It does whatever it needs to gather the HTML for the story to be output.
  • tad.pl outputs javascript. In fact, tad.pl's output is nothing more than a series of "document.write" statements.
  • The client browser executes the javascript. The result is that it prints the HTML that tad.pl constructed for today's story.

Using tad.pl in a server-side include looks like this:

<!--#include VIRTUAL="/cgi-bin/proxy.pl?http://www.thisistrue.net/cgi-bin/tad.pl?ssi=1" -->

Note that as discussed above we had to use a local CGI program, proxy.pl, to fetch the remote URL.

Note also that we've added an additional parameter, ssi=1. This causes tad.pl to output the HTML it gathers without the javascript document.write statements. The final sequence looks like this:

  • proxy.pl executes on your server, and is passed the URL "http://www.thisistrue.net/cgi-bin/tad.pl?ssi=1".
  • proxy.pl fetches that URL, causing tad.pl to execute on www.thisistrue.net.
  • tad.pl once again does whatever it needs to gather the HTML for the story to be output.
  • tad.pl outputs the HTML to be included.
  • That output is returned to proxy.pl, which in turn prints it, where it becomes the "included text".
  • The client browser sees nothing but HTML - the HTML from the containing page with the HTML from tad.pl inserted in place of the include statement.

As you can see, includes are not only a great organizational tool allowing you to collect common information into fewer files, but also a powerful way to add functionality to your web site and perhaps others. I'll end this with True-A-Day included via a client side include:

<script src="http://www.thisistrue.net/cgi-bin/tad.pl" type="text/javascript"></script> rel="stylesheet" href="http://thisistrue.net/tad.css" type="text/css">

This is True®

by Randy Cassingham

Stories from Our Archives ©1994-2008

Office Accessory
Since 1911, every Rolls Royce has had a hood ornament called �The Spirit of Ecstasy�. The Baron Montagu of Beaulieu has now revealed the model for the ornament. It was commissioned by his father, the 2nd Baron Montagu, who was editor of The Car magazine and the owner of a 1907 Rolls Silver Ghost. �The model... was my father�s secretary, Eleanor Thornton,� the current Baron says. �Actually,� he adds, �she was a little more than a secretary.� (AP) ...In 1907, that�s a reverent tribute. In 1997, it�s a sexual harassment lawsuit.

Subscribe Free

Get This is True by e-mail once a week



Visit TrueTrue Story collections
Get This Service for Your Site

<script style="display: none;" language="JavaScript" type="text/javascript" src="http://ads.pugetsoundsoftware.com/adx.js"></script> <script language="JavaScript" type="text/javascript"> </script><script style="display: none;" language="JavaScript" type="text/javascript" src="http://ads.pugetsoundsoftware.com/adjs.php?n=602498587&what=adblock3,_entryid149%7Cadblock3,_web%7Cadblock3,_web_site_management%7Cadblock3def&exclude=,&referer=http%3A//www.google.cn/search%3Fhl%3Dzh-CN%26newwindow%3D1%26q%3Dhtml+include+file%26btnG%3DGoogle+%25E6%2590%259C%25E7%25B4%25A2%26meta%3D%26aq%3D1%26oq%3Dhtml+include"></script>

Article 149 | Posted February 8, 2004

Recent Comments
74 Comments

Hmm, couldn't get any of these to work.

Posted by: Dummy at January 17, 2008 9:25 AM

Is there a way to include a file into a marquee? Or have a marquee fit to page?

Posted by: Seth at January 23, 2008 10:36 PM

I finally got my problem solved, which was not only a client side include, but a client side include where I had to calculate which include to include. (Did I make myself clear or what? ;-> )
I wanted to include the daily watchwords (see www.ebu.org) in my "Outlook Today" page.

This is my code (I hope it shows well):


function fuehrendeNull(wert)
{
if (wert else return parseInt(wert);
}
var LosSrc
var wt = new Array("Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag");
var d = new Date();
var t = d.getDate();
var m = d.getMonth() + 1;
var j = d.getYear();
if(j LosSrc="Losungen/" + j + "/" + fuehrendeNull(m) + "/de" + fuehrendeNull(j -2000) + fuehrendeNull(m) + fuehrendeNull(t) + ".js"
document.write("")

If the cite-tags are showed, they are the ones which shouldn't show...
If it doesn't show at all or only very strange, my trick is to do a "document.write" that writes ANOTHER "script"-Statement, in which the variable is inserted.

Posted by: Volker at January 24, 2008 12:43 AM

Well, the code didn't show really well.
Here it is again, only with much less

#script language="JavaScript" type="text/javascript"#
function fuehrendeNull(wert)
{
if (wert else return parseInt(wert);
}
var LosSrc
var wt = new Array("Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag");
var d = new Date();
var t = d.getDate();
var m = d.getMonth() + 1;
var j = d.getYear();
if(j LosSrc="Losungen/" + j + "/" + fuehrendeNull(m) + "/de" + fuehrendeNull(j -2000) + fuehrendeNull(m) + fuehrendeNull(t) + ".js"
document.write("#script src=/""+ LosSrc +"/" language=/"JavaScript/" type=/"text/javascript/"##//script#")
#/script#

BTW, I got the URL of the watchwords wrong - sorry! The right ones are: www.losungen.de and www.moravian.org - God bless you!

Posted by: Volker at January 24, 2008 12:53 AM

Html Attribute for -- Marquee Slide Image and Text ---
http://html-lesson.blogspot.com/2008/06/marquee-slide-image-text.html

Posted by: sezer at July 13, 2008 12:41 PM

for apache, when you want to use Server Side Includes, with the default configuration, you will have to have your file 'file.shtml' and not just 'file.html'. This is easy to trip over when you first start.
I believe that you can have apache look at all 'html' files for includes, but there is a penalty for this, which I can't recall -could be efficiency.

Posted by: Henry Leparskas at July 16, 2008 8:36 AM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I've found that ".shtml", while common, is not at all
standard. It definitely pays to understand your specific web
server's configuration.

Also, scanning all .html (and .inc) is definitely possible,
and not a large performance hit at all. This site, for
example, is configured that way.

Leo


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)

iD8DBQFIf33UCMEe9B/8oqERAgHjAJ4vvXyNhWpDmcRM5pHuHK5dp1Hi1ACgiVua
BPNQ1KC/NkFsfqPPBwRR0xI=
=KL4T
-----END PGP SIGNATURE-----

Posted by: Leo at July 17, 2008 10:14 AM

Couldn't get the included file to display correctly. Used php require_once("BaseMenu.php"): You can see the problem at URL: http://medical.julseth.net/disMP.php

Posted by: Bruce A. Julseth at August 3, 2008 7:54 PM

I have used both "html include" and "php readfile" clauses and found them working marvelously. Thanks.

I want that contents of included file must not be visible by viewer's "View Page Source".

Thanks once again

MCB

Posted by: mcbothra99 at August 5, 2008 8:42 PM

Even though I saw several negative comments about your suggestions, I was desperate, and tried it anyway. I use the line and it worked perfectly. THANK YOU! I changed the name of the file to express the .inc extension instead of the .html extension. What a charm!

But again, THANKS. This was a life-saver!

H. C. Greene

Posted by: H. G. Greene at October 18, 2008 7:37 PM
See all 74 comments...

Post a comment on "How do I include one HTML file inside another?":






(Email Address will not be published.)

Remember Me?

By popular demand...
my tip jar
cuppa joe
Buy Leo a Latte!

(you may use HTML tags for style)

New!

rss feed Subscribe to the RSS Feed specifically for comments on this article.

Before commenting, please...

  • Read the article at the top of this page. If your comment shows you didn't, it'll be deleted and ignored.

  • Comment only on this article. Use the Google search box at the top of the page if you have a question about something else.

  • Don't include personal information in the comment. No email addresses. No phone numbers. No physical addresses.

  • Don't ask me to recover lost passwords or hacked accounts. I can't, and those comments will be deleted.

  • can't respond to every comment. And I can't vouch for the accuracy of others who do.

  • Read Why didn't you answer my question? for hints on getting an answer.

  • By posting a comment you agree to the Terms of Service. Don't agree? Don't post.

  • Read the article at the top of this page. If your comment shows you didn't, it'll be deleted and ignored. Yes, I'm saying this twice; you'd be amazed.

Please wait. Your comment is being processed ...

<script type="text/javascript" language="javascript"> if (document.comments_form.email != undefined) document.comments_form.email.value = getCookie("mtcmtmail"); if (document.comments_form.author != undefined) document.comments_form.author.value = getCookie("mtcmtauth"); if (document.comments_form.url != undefined) document.comments_form.url.value = getCookie("mtcmthome"); if (getCookie("mtcmtauth") || getCookie("mtcmthome")) { document.comments_form.bakecookie[0].checked = true; } else { document.comments_form.bakecookie[1].checked = true; } </script> <script type="text/javascript" language="javascript" src="http://ask-leo.com/comment.js"> </script>

<script type="text/javascript"> </script> <script style="display: none;" type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

Ask Your Question:


ask-leo.com
Web

<script type="text/javascript"> </script> <script style="display: none;" type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

Stay Informed

Weekly Newsletter

Archives

By Category
By Date

Advertisers

Advertise on Ask Leo!

««   »»

Question? - Ask Leo!
Who is Leo?
Link to Leo!

Terms, Conditions & Privacy

<script type="text/javascript"> var dc_UnitID = 14; var dc_PublisherID = 10483; var dc_AdLinkColor = 'blue'; var dc_isBoldActive= 'no'; var dc_adprod='ADL'; </script> <script style="display: none;" type="text/javascript" src="http://kona.kontera.com/javascript/lib/KonaLibInline.js"></script> <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-302170-1"; urchinTracker(); </script> <script type="text/javascript" src="http://analytics.aweber.com/js/awt_analytics.js?id=7qbL"></script> <script style="display: none;" type="text/javascript" src="http://forms.aweber.com/form/39/356439.js"></script>

最后

以上就是迷路万宝路为你收集整理的在html页面中包含共享页面How do I include one HTML file inside another?的全部内容,希望文章能够帮你解决在html页面中包含共享页面How do I include one HTML file inside another?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(34)

评论列表共有 0 条评论

立即
投稿
返回
顶部