在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 相关文章推荐
超强的IE背景图片闪烁(抖动)的解决办法
Sep 09 Javascript
运用jquery实现table单双行不同显示并能单行选中
Jul 25 Javascript
JavaScript聚焦于第一个字段的代码
Oct 15 Javascript
ExtJS4利根据登录后不同的角色分配不同的树形菜单
May 02 Javascript
jQuery不兼容input的change事件问题解决过程
Dec 05 Javascript
JS实现选择TextArea内文本的方法
Aug 03 Javascript
div实现自适应高度的textarea实现angular双向绑定
Jan 08 Javascript
vue.js2.0 实现better-scroll的滚动效果实例详解
Aug 13 Javascript
如何为vue的项目添加单元测试
Dec 19 Javascript
Vue infinite update loop的问题解决
Apr 23 Javascript
Vant picker 多级联动操作
Nov 02 Javascript
基于javascript实现移动端轮播图效果
Dec 21 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 生成饼图 三维饼图
2009/09/28 PHP
php删除二维数组中的重复值方法
2018/03/12 PHP
ThinkPHP实现的rsa非对称加密类示例
2018/05/29 PHP
不用写JS也能使用EXTJS视频演示
2008/12/29 Javascript
JavaScript和ActionScript的交互实现代码
2010/08/01 Javascript
基于JQuery的一个简单的鼠标跟随提示效果
2010/09/23 Javascript
node.js中的path.dirname方法使用说明
2014/12/09 Javascript
JavaScript包装对象使用详解
2015/07/09 Javascript
js实现的二级横向菜单条实例
2015/08/22 Javascript
原生JS下拉加载插件分享
2016/12/26 Javascript
react 父组件与子组件之间的值传递的方法
2017/09/14 Javascript
Angular实现点击按钮后在上方显示输入内容的方法
2017/12/27 Javascript
React组件对子组件children进行加强的方法
2019/06/23 Javascript
JavaScript实现单图片上传并预览功能
2019/09/30 Javascript
Python实现SVN的目录周期性备份实例
2015/07/17 Python
Python ftp上传文件
2016/02/13 Python
从请求到响应过程中django都做了哪些处理
2018/08/01 Python
python list转矩阵的实例讲解
2018/08/04 Python
Python类的继承、多态及获取对象信息操作详解
2019/02/28 Python
详解用Python实现自动化监控远程服务器
2019/05/18 Python
pymysql 插入数据 转义处理方式
2020/03/02 Python
如何用H5实现一个触屏版的轮播器的实例
2017/01/09 HTML / CSS
HTML5 Blob 实现文件下载功能的示例代码
2019/11/29 HTML / CSS
AmazeUI 手机版页面的顶部导航条Header与侧边导航栏offCanvas的示例代码
2020/08/19 HTML / CSS
Internet体系结构
2014/12/21 面试题
生产车间实习自我鉴定
2013/09/23 职场文书
中学生团员自我评价分享
2013/12/07 职场文书
培训班主持词
2014/03/28 职场文书
2014卖家双十一活动策划书
2014/09/29 职场文书
五一劳动节慰问信
2015/02/14 职场文书
交通事故案件代理词
2015/05/23 职场文书
2015年税务稽查工作总结
2015/05/26 职场文书
关于Oracle12C默认用户名system密码不正确的解决方案
2021/10/16 Oracle
Minikube搭建Kubernetes集群
2022/03/31 Servers
详解OpenCV曝光融合
2022/04/29 Python
CSS实现背景图片全屏铺满自适应的3种方式
2022/07/07 HTML / CSS