Meta http-equiv属性详解

HTML meta标签 http-equiv属性详解

http-equiv 给浏览器提供文档级指令,其中部分名称与 HTTP 响应头相似。它不会让服务器增加或修改响应头,也不能替代服务器、CDN 或反向代理的响应头配置。

下文保留 2018 年整理的历史示例,逐项说明当前支持边界。不要假定任意 HTTP 头名称都能通过 <meta http-equiv> 生效,尤其是缓存、Cookie 和安全策略。

meta标签的 http-equiv 属性语法格式如下:

1
<meta http-equiv="参数" content="参数变量">

其中http-equiv属性主要有以下几种参数:

Expires(期限)

这是历史写法,不应依赖它控制当前浏览器或共享缓存。实际过期策略应在 HTTP 响应中配置 Expires 或 Cache-Control,过期也不意味着一定重新下载全部响应体。
历史示例:

1
<meta http-equiv="expires" content="Wed, 20 Jun 2007 22:33:00 GMT">

注意:必须使用GMT的时间格式。

Pragma(cache模式)

这是历史缓存写法,不保证现代浏览器支持,也不能配置代理或 CDN 缓存。应使用 HTTP Cache-Control 响应头;no-cache 允许存储,但再次使用前需要验证,no-store 才表示不要存储。
历史示例:

1
<meta http-equiv="Pragma" content="no-cache">

不能据此保证清空缓存或禁止离线浏览。

Refresh(刷新)

说明:自动刷新并指向新页面。
用法:

1
<meta http-equiv="Refresh" content="5;URL=https://m.zol.com.cn/">

现代浏览器忽略 http-equiv="Set-Cookie",不能通过它设置或删除 Cookie。服务器应发送真正的 Set-Cookie 响应头;脚本可通过 document.cookie 设置有权限访问的 Cookie,但无法读写 HttpOnly Cookie。
下面只保留历史示例,不作为当前可用方案:

1
<meta http-equiv="Set-Cookie" content="cookievalue=xxx;expires=Tue Mar 20 2018 14:31:40 GMT+0800 (CST); path=/">

Cookie 的有效期与网页缓存过期不是同一件事;需要删除 Cookie 时,应按相同名称、Domain 和 Path 设置过期 Cookie。

Window-target(显示窗口的设定)

该历史 meta 写法不能作为现代浏览器的防嵌套措施。要限制页面被 frame/iframe 嵌入,应配置 HTTP Content-Security-Policy 的 frame-ancestors 指令,或按兼容要求配置 X-Frame-Options 响应头。frame-ancestors 也不能通过 CSP meta 标签生效。
历史示例:

1
<meta http-equiv="Window-target" content="_top">

例如由服务器返回 Content-Security-Policy: frame-ancestors 'none',而不是依赖 Window-target。

content-Type(显示字符集的设定)

说明:设定页面使用的字符集。
用法:

1
<meta http-equiv="content-Type" content="text/html; charset=utf-8">

Pics-label(网页等级评定)

用法:

1
<meta http-equiv="Pics-label" contect="">  

这是旧 IE 的内容分级机制,不是现代浏览器支持的访问控制措施。

Page_Enter、Page_Exit

以下为旧 IE 的页面转场写法,现代浏览器不应依赖它。历史进入页面示例:

1
<meta http-equiv="Page-Enter"    contect="revealTrans(duration=1.0,transtion=12)">

设定离开页面时的特殊效果

1
<meta http-equiv="Page-Exit"    contect="revealTrans(duration=1.0,transtion=12)">

Duration的值为网页动态过渡的时间,单位为秒。
Transition是过渡方式,它的值为0到23,分别对应24种过渡方式。如下表:

Transition值 释义
0 盒状收缩
1 盒状放射
2 圆形收缩
3 圆形放射
4 由下往上
5 由上往下
6 从左至右
7 从右至左
8 垂直百叶窗
9 水平百叶窗
10 水平格状百叶窗
11 垂直格状百叶窗
12 随意溶解
13 从左右两端向中间展开
14 从中间向左右两端展开
15 从上下两端向中间展开
16 从中间向上下两端展开
17 从右上角向左下角展开
18 从右下角向左上角展开
19 从左上角向右下角展开
20 从左下角向右上角展开
21 水平线状展开
22 垂直线状展开
23 随机产生一种过渡方式

缓存控制(历史 meta 写法)

下面的 meta 不能替代 HTTP Cache-Control 响应头。即使在响应头中使用 no-cache,验证未变化后也可能返回 304 并复用已存储的响应体,不等于每次完整下载。

1
<meta http-equiv="cache-control" content="no-cache">  

设定网页的到期时间

以下也属于历史 meta 写法,当前应在 HTTP 响应中设置缓存策略。

1
<meta http-equiv="expires" content="0">

关键字,给搜索引擎用的

1
<meta name="keywords" content="keyword1,keyword2,keyword3">

页面描述

1
<meta name="description" content="This is my page">

HTML DOM httpEquiv 属性

定义

HTMLMetaElement.httpEquiv 反映元素的 http-equiv 属性值,读取或修改它不会修改服务器的 HTTP 响应头。

语法:

1
metaObject.httpEquiv = "refresh";

实例

本例每 5 秒刷新页面

1
2
3
4
5
6
7
8
9
10
11
12
13
<html>
<head>
<meta http-equiv="refresh" content="5" />
</head>
<body>

<script type="text/javascript">
x=document.getElementsByTagName("meta")[0];
document.write("Http equiv: " + x.httpEquiv);
</script>

</body>
</html>

参考资料

本文永久链接: https://www.mulianju.com/http-equiv/