在html页面中包含共享页面的方法


Posted in Javascript onOctober 24, 2008

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.
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:

Javascript 相关文章推荐
javascript 动态参数判空操作
Dec 22 Javascript
IE6-IE9不支持table.innerHTML的解决方法分享
Sep 14 Javascript
JQuery右键菜单插件ContextMenu使用指南
Dec 19 Javascript
原生js实现的贪吃蛇网页版游戏完整实例
May 18 Javascript
小议JavaScript中Generator和Iterator的使用
Jul 29 Javascript
jQuery Validate表单验证入门学习
Dec 18 Javascript
BootStrap响应式导航条实例介绍
May 06 Javascript
预防网页挂马的方法总结
Nov 03 Javascript
原生js实现选项卡功能
Mar 08 Javascript
vue中页面跳转拦截器的实现方法
Aug 23 Javascript
JavaScript Date对象应用实例分享
Oct 30 Javascript
angularJs中json数据转换与本地存储的实例
Oct 08 Javascript
IE浏览器兼容Firefox的JS脚本的代码
Oct 23 #Javascript
Javascript客户端将指定区域导出到Word、Excel的代码
Oct 22 #Javascript
checkbox 多选框 联动实现代码
Oct 22 #Javascript
javascript网页关闭时提醒效果脚本
Oct 22 #Javascript
javascript Select标记中options操作方法集合
Oct 22 #Javascript
JavaScript Undefined,Null类型和NaN值区别
Oct 22 #Javascript
javascript TextArea动态显示剩余字符
Oct 22 #Javascript
You might like
PHP生成静态页面详解
2006/11/19 PHP
那些年一起学习的PHP(三)
2012/03/22 PHP
php使用Session和文件统计在线人数
2015/07/04 PHP
PHP中strnatcmp()函数“自然排序算法”进行字符串比较用法分析(对比strcmp函数)
2016/01/07 PHP
PHP下 Mongodb 连接远程数据库的实例代码
2017/08/30 PHP
js实现的跟随鼠标移动的时钟效果(中英文日期显示)
2011/01/17 Javascript
30个最佳jQuery Lightbox效果插件分享
2011/04/11 Javascript
关于html+ashx开发中几个问题的解决方法
2011/07/18 Javascript
兼容IE、FireFox、Chrome等浏览器的xml处理函数js代码
2011/11/30 Javascript
仅IE支持clearAttributes/mergeAttributes方法使用介绍
2012/05/04 Javascript
父节点获取子节点的字符串示例代码
2014/02/26 Javascript
javasciprt下jquery函数$.post执行无响应的解决方法
2014/03/13 Javascript
js判断文本框剩余可输入字数的方法
2015/02/04 Javascript
NodeJS中利用Promise来封装异步函数
2015/02/25 NodeJs
jsp 自动编译机制详细介绍
2016/12/01 Javascript
Angular2自定义分页组件
2017/04/19 Javascript
Vue中的Vux配置指南
2017/12/08 Javascript
解决Vue 通过下表修改数组,页面不渲染的问题
2018/03/08 Javascript
vue改变循环遍历后的数据实例
2019/11/07 Javascript
12 种使用Vue 的最佳做法
2020/03/30 Javascript
vue 如何从单页应用改造成多页应用
2020/10/23 Javascript
原生JavaScript实现进度条
2021/02/19 Javascript
Python 获得命令行参数的方法(推荐)
2018/01/24 Python
Python动态赋值的陷阱知识点总结
2019/03/17 Python
python fuzzywuzzy模块模糊字符串匹配详细用法
2019/08/29 Python
全球领先的鞋类零售商:The Walking Company
2016/07/21 全球购物
Viking Direct爱尔兰:办公用品和家具
2019/11/21 全球购物
自荐书封面下载
2013/11/29 职场文书
简历里的自我评价范文
2014/02/24 职场文书
美德少年事迹材料1000字
2014/08/21 职场文书
关于运动会广播稿50字
2014/10/18 职场文书
不尊敬老师检讨书范文
2014/11/19 职场文书
解约证明模板
2015/06/19 职场文书
如何起草一份正确的合伙创业协议书?
2019/07/04 职场文书
vue 实现上传组件
2021/05/31 Vue.js
MySql按时,天,周,月进行数据统计
2022/08/14 MySQL