包括常见的html代码 [英] Include common html code

查看:69
本文介绍了包括常见的html代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚递交了一个旧的html网站进行更新(不升级!)。

我无法重写整个事情,这本来是我的偏好!



我要求做的是在菜单中添加一个新选项,该选项显示在网站的每个页面顶部(其中40个)。



目前构建菜单的html是用 每个页面手工编码的!



有没有办法在原始HTML中使用相当于php的包含?



我遇到了

 <! -     #include virtual =/ menu.shtml   - >   

但似乎无法让它发挥作用;不知道哪个浏览器支持它。

当然有办法做到这一点?我不想修改40页,这很无聊!



我认为该网站很快就需要另一个菜单选项,所以我宁愿找到一种方法来嵌入html。



帮助...



我的尝试:



我尝试使用#include但没有成功

解决方案

没有包含对HTML的支持。



有些HTML编辑器支持自己的方式,在注释中放置特殊命令并在保存时替换内容。但这对你当前的任务没有帮助,因为你必须至少编辑一次所有文件,并且必须在以后总是使用特定的编辑器。



但你可能会使用文本编辑器,提供多个文件的搜索和替换(全部打开或在目录中全部匹配)。使用这样的编辑器在新条目之前或之后搜索现有的菜单条目代码,并用旧代码替换后面/前面的新代码。首先尝试在编辑器中打开一个文件并在应用于所有文件之前检查结果,因为大多数编辑器不支持撤消对多个文件的操作。





提供此类操作的编辑器是Notepad ++。



您提到了SSI(服务器端包含)。这些由Web服务器处理(由名称指示),Web服务器在传递页面内容时将加载文件内容(或可变内容或脚本的输出)。必须通过Web服务器配置启用它,并且HTML文件必须是可执行的(通过文件系统位或使用特定扩展名)。它不适用于加载浏览器的本地文件,需要Web服务器支持,并且需要访问Web服务器配置。



所以你可以使用如果您有能力更改Web服务器配置。

[/ EDIT]


仅供参考。支持 。我开始写一个新网站并再次尝试使用include,它运行得很好。



我使用的语法是

<! -  #include file =/ commonHTML / header.html - > 



其中commonHTML是项目级文件夹。



如果您认为有需要,您甚至可以在包含内嵌入包含。

我在一个JavaScript中嵌入了JavaScript,其中JavaScript仅适用于HTML包含文件所以它只在需要它的页面上加载。



我已经在几个平台上使用多个浏览器对它进行了测试,所有人似乎都支持它,所以我假设包含是在服务器上执行的,因此所有页面都正常呈现。

不确定为什么我的原始测试失败了?它可能是一个IIS设置或App Pool中知道/关心的东西;有用! Yipee


I have just been handed an old html web site to update (not upgrade!).
I am not in a position to rewrite the whole thing which would have been my preference!

What I have been asked to do is add a new option in the menu which appears at the top of every page (40 of them) in the site.

Currently the html to build the menu is hand coded in every page!

Is there a way to use the equivalent of php's include in raw html?

I came across

<!--#include virtual="/menu.shtml" --> 

but cannot seem to get it to work; not sure which browsers support it anyway.
Surely there is a way to do this? I don't want to amend 40 pages it's boring!

I think the site will need another menu option soon so I would rather find a way to embed html now.

Help...

What I have tried:

I have tried using #include but with no success

解决方案

There is no include support with HTML.

Some HTML editors support there own way of doing this with special commands placed inside comments and replacing the content upon saving. But this will not help you with your current task because you have to edit all files at least once and must always use the specific editor later.

But you might use a text editor that provides search and replace for multiple files (all opened or all matching within a directory). With such an editor search for the existing menu entry code just before or after the new entry and replace with the old code followed / prefixed with the new one. Try first with a single file opened in the editor and check the result before applying to all files because most editors does not support undo for operations on multiple files.

[EDIT]
An editor that provides such operations is Notepad++.

You mentioned SSI (Server Side Includes). These are handled (as indicated by the name) by the web server which will load the file content (or variable contents or the output of an script) when delivering the page content. It must be enabled by web server configuration and the HTML file must be executable (by file system bit or using a specific extension). It will not work for local files loaded with the browser, requires to be supported by the web server, and requires access to the web server configuration.

So you might use it if you are in a position to change the web server configuration.
[/EDIT]


Just for info. It is supported. I started writing a new website and tried the include again and it works just fine.

Syntax I used is

<!-- #include file ="/commonHTML/header.html" -->


Where commonHTML is a project level folder.

You can even embed includes within includes if you feel the need.
I have embedded JavaScript in one also where the JavaScript only applied to the HTML in that include file so it only loads on pages that need it.

I have tested it on several platforms using several browsers and all seem to support it just fine so I assume that the include is performed on the server and therefore all pages are rendered as normal.
Not sure why my original test failed? It may be an IIS setting or something in the App Pool who knows / cares; it works! Yipee


这篇关于包括常见的html代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆