How do I include one HTML file inside another?
It's very common practice to have a consistent on a Web site. You might have a standard navigation bar or a logos or even just a page footer with copyright and administrative informatio N. Rather than actually has that information in each and every page it would certainly is nice if you could write your Navigation bar once, keep it in one file, and then reference ' 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 doing this, and so much, 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 JAVASCRI Pt. 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'll 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 a example in a HTML file a.html we can place this line:
<!--#include file= "B.inc"-->
The page seen by a browser viewing a.html would consist of the contents of a.html the include line, before by the Contents of B.inc, followed by the contents of a.html after the include line. Put the HTML to 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 could needed but even if you don ' t have access to the actual server configuration files it C An typically also is enabled by commands in a file named. htaccess, you'll either find or can create in your server ' s web directory. Read more about the Apache SSI here. Under IIS, the SSI is enabled anytime your use '. asp ' pages-so the ' only configuration ' you need ' to name your pages. asp Instead of. html. Read more about the Server Side Include in ASP pages.
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 Abov E work is to have PHP on your Web server and name your file ". PHP".
With the above approaches, the browser viewing the page knows absolutely nothing about the include-it D before the page was downloaded. However, sometimes processing a 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 script might generat e as if it were inline HTML. Technically that's not a include but the script "B.js" could to no more than a series of JavaScript "print" Statemen TS such as these:
document.write ("<table>")
document.write ("<tr>")
... and on
You can have a "printing" the HTML you want included. In the other words, if your can format your include file as a series of JavaScript prints, can use client-side include to I Nsert it.
Now things can get very interesting, because we'll introduce two things:remote, and CGI includes, into the mix.
Remote Includes
The files we ' ve included so far have been assumed to is on your own server in the same location as your other HTML pages. In almost all cases your can "include" using a full URL to the 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'll get loaded from example.com rather than your 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.
CGI Includes
So far we ' ve included only "Static" HTML pages. As it turns out your can "include" the output of a Server-run CGI program. This then becomes we solution for Apache ' s lack of the 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 is appropriately installed on your server to your directory or its cgi-bin. By being the 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 a Apache remote include this way:
<!--#include virtual= "/cgi-bin/proxy.pl?http://example.com/b.inc"-->
It works as 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 this contents of B.inc show up as a 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 have all of these pieces together we can create some very useful and interesting Internet applications.
Randy Cassingham of this are True wanted to being able to provide he readers who had web sites the ability to host one Stories. Sounds like a job for a include file, right? But He also wanted the story to change every day. That ' s more than a static HTML page; That's a CGI program which outputs a different story each day.
The result is tad.pl (true-a-day)-a Perl script this picks a new story every day and outputs HTML. Given what we ' ve talked about so far I 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 simplest form. Note:
Tad.pl is written in Perl. It does whatever it needs to gather the HTML for the story to is output.
tad.pl outputs JavaScript. In fact, tad.pl's output is no more than a series of "document.write" statements.
The client browser executes the JavaScript. The "result is" that it prints the HTML this tad.pl constructed for today ' s story.
Using tad.pl in a server-side include looks as this:
<!--#include virtual= "/cgi-bin/proxy.pl?http://www.thisistrue.net/cgi-bin/tad.pl?ssi=1"-->
Note this as discussed above we had to use a local CGI program, proxy.pl, to fetch the remote URL.
Note also so we ' ve added an additional parameter, Ssi=1. This is 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 as the story to is output.
tad.pl outputs the HTML to be included.
That's returned to proxy.pl, which in turn prints it, where it becomes the "included text".
The client browser sees nothing but html-the HTML to the containing page with the HTML from tad.pl to place O f the Include statement.
As you can be, includes are not only a great organizational tool allowing you to collect common information into fewer fi Les, 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:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.