在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 相关文章推荐
什么是DOM(Document Object Model)文档对象模型
Mar 05 Javascript
判断javascript的数据类型(示例代码)
Dec 11 Javascript
三种动态加载js的jquery实例代码另附去除js方法
Apr 30 Javascript
javascript基于DOM实现权限选择实例分析
May 14 Javascript
jQuery插件支持同一页面被多次调用
Feb 14 Javascript
Bootstrap3.0建站教程(一)之bootstrap表单元素排版
Jun 01 Javascript
vue与TypeScript集成配置最简教程(推荐)
Oct 17 Javascript
vue实现某元素吸顶或固定位置显示(监听滚动事件)
Dec 13 Javascript
vue 动态改变静态图片以及请求网络图片的实现方法
Feb 07 Javascript
微信小程序学习笔记之登录API与获取用户信息操作图文详解
Mar 29 Javascript
vue.js高德地图实现热点图代码实例
Apr 18 Javascript
React-vscode使用jsx语法的问题及解决方法
Jun 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信息函数
2015/10/21 PHP
php实用代码片段整理
2016/11/12 PHP
微信公众平台开发教程④ ThinkPHP框架下微信支付功能图文详解
2019/04/10 PHP
tp5 sum某个字段相加得到总数的例子
2019/10/18 PHP
很全的显示阴历(农历)日期的js代码
2009/01/01 Javascript
javascript之bind使用介绍
2011/10/09 Javascript
javascript中自定义对象的属性方法分享
2013/07/12 Javascript
jQuery实现的一个自定义Placeholder属性插件
2014/08/11 Javascript
jQuery实现弹出窗口中切换登录与注册表单
2015/06/05 Javascript
全面解析Bootstrap表单使用方法(表单控件状态)
2015/11/24 Javascript
vue,angular,avalon这三种MVVM框架优缺点
2016/04/27 Javascript
设置点击文本框或图片弹出日历控件的实现代码
2016/05/12 Javascript
canvas时钟效果
2017/02/16 Javascript
angular-ngSanitize模块-$sanitize服务详解
2017/06/13 Javascript
微信小程序 开发MAP(地图)实例详解
2017/06/27 Javascript
Bootstrap 模态框(Modal)带参数传值实例
2017/08/20 Javascript
安装vue-cli报错 -4058 的解决方法
2017/10/19 Javascript
详解一个基于套接字实现长连接的express
2019/03/28 Javascript
微信小程序间使用navigator跳转传值问题实例分析
2020/03/27 Javascript
为什么推荐使用JSX开发Vue3
2020/12/28 Vue.js
Python中的推导式使用详解
2015/06/03 Python
Python缩进和冒号详解
2016/06/01 Python
Python使用asyncio包处理并发详解
2017/09/09 Python
对python 矩阵转置transpose的实例讲解
2018/04/17 Python
OpenCV-Python 摄像头实时检测人脸代码实例
2019/04/30 Python
python sorted方法和列表使用解析
2019/11/18 Python
Python3之外部文件调用Django程序操作model等文件实现方式
2020/04/07 Python
Python 处理日期时间的Arrow库使用
2020/08/18 Python
高中毕业自我鉴定
2013/12/22 职场文书
上海世博会口号
2014/06/19 职场文书
2014年信息宣传工作总结
2014/12/18 职场文书
网络营销计划书
2015/01/17 职场文书
祝酒词范文
2015/08/12 职场文书
2016年幼儿园教研活动总结
2016/04/05 职场文书
Go 实现英尺和米的简单单位换算方式
2021/04/29 Golang
Python办公自动化之教你用Python批量识别发票并录入到Excel表格中
2021/06/26 Python