How to include a shared page in an html page

Source: Internet
Author: User
Tags perl script microsoft iis

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 wocould certainly be nice if you cocould 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 completed MED 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 encryption DES
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. 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 abve 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 encryption DES
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 "cocould 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 encoded ded. 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 controls des, and CGI programs, into the mix.
Remote Federation des
The files we 've already ded 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 nodes des 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 accesses des. But there's almost always a way and we have a workaround using CGI.
CGI shortdes
So far we 've got ded 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 clients. we'll start with this very short Perl program:
Use LWP: Simple;
Print "Content-type: text/html \ 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 attached ded file.
Includes + remote includes + CGI
So far we 've used a CGI program to fetch what is essential 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 abve 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 encoded ded.
That output is returned to proxy. pl, which in turn prints it, where it becomes the "encoded 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, your des 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 encoded by a client side include:

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.