--- title: Feature-Policy slug: Web/HTTP/Headers/Feature-Policy translation_of: Web/HTTP/Headers/Feature-Policy ---
{{HTTPSidebar}} {{SeeCompatTable}}
 
Feature-Policy响应头提供了一种可以在本页面或包含的iframe上启用或禁止浏览器特性的机制。

更多的信息,请查看Feature Policy

Header type {{Glossary("Response header")}}
{{Glossary("Forbidden header name")}} yes

语法

Feature-Policy: <directive> <allowlist>

<allowlist>

*(在所有源地址启用)'none'(在所有源地址禁用)只允许单独使用,而'self''src'可以与多个源地址一起使用。

所有的特性都有一个如下的默认的allowlist

指令

{{httpheader('Feature-Policy/autoplay','autoplay')}}
控制是否允许当前文档自动播放媒体。这种控制是通过接口 {{domxref("HTMLMediaElement")}} 来实现。当这种规则被启用,而且没有用户操作的时候,{{domxref("HTMLMediaElement.play()")}}返回的 {{domxref("Promise")}}会拒绝并抛出一个DOMException异常。在{{HTMLELement("audio")}}和{{HTMLELement("video")}}上的autoplay属性会被忽略。
{{httpheader('Feature-Policy/camera', 'camera')}}
控制是否允许当前文档使用视频输入设备。当这种规则被启用时,{{domxref("MediaDevices.getUserMedia()")}}返回的the {{jsxref("Promise")}}会拒绝并抛出错误NotAllowedError。
{{httpheader('Feature-Policy/document-domain','document-domain')}}
控制是否允许当前文档设置{{domxref("document.domain")}}。当这种规则被启用时,尝试设置{{domxref("document.domain")}}会失败并抛出SecurityError {{domxref("DOMException")}}异常。
{{httpheader('Feature-Policy/encrypted-media', 'encrypted-media')}}
控制是否允许当前文档使用Encrypted Media Extensions API (EME)。当这种规则被启用时,{{domxref("Navigator.requestMediaKeySystemAccess()")}}返回的{{domxref("Promise")}}会拒绝并抛出DOMException异常。
{{httpheader('Feature-Policy/fullscreen','fullscreen')}}
控制是否允许当前文档使用{{domxref('Element.requestFullScreen()')}}。当这种规则被启用时,返回的{{jsxref('Promise')}}会拒绝并抛出{{jsxref('TypeError')}}。
{{httpheader('Feature-Policy/geolocation','geolocation')}}
控制是否允许当前文档使用{{domxref('Geolocation')}}接口。当这种规则被启用时,调用{{domxref('Geolocation.getCurrentPosition','getCurrentPosition()')}}和{{domxref('Geolocation.watchPosition','watchPosition()')}}会返回包含PERMISSION_DENIED的 {{domxref('PositionError')}}。
{{httpheader('Feature-Policy/microphone','microphone')}}
控制是否允许当前文档使用音频输入设备。当这种规则被启用时,{{domxref("MediaDevices.getUserMedia()")}}返回的the {{jsxref("Promise")}}会拒绝并抛出错误NotAllowedError。
{{httpheader('Feature-Policy/midi', 'midi')}}
控制是否允许当前文档使用Web MIDI API。当这种规则被启用时,{{domxref("Navigator.requestMIDIAccess()")}} 返回的the {{jsxref("Promise")}}会拒绝并抛出错误DOMException。
{{httpheader('Feature-Policy/payment', 'payment')}}
控制是否允许当前文档使用Payment Request API。当这种规则被启用时,构造器{{domxref("PaymentRequest()")}} 会抛出错误SecurityError。
{{httpheader('Feature-Policy/vr', 'vr')}} / xr
控制是否允许当前文档使用WebVR API。当这种规则被启用时,{{domxref("Navigator.getVRDisplays()")}} 返回的the {{jsxref("Promise")}}会拒绝并抛出错误DOMException。

示例

SecureCorp Inc. 公司想要在应用中禁用震动和定位API,则可以在返回的response中传递以下定义feature policy的HTTP的头部信息:

Feature-Policy: vibrate 'none'; geolocation 'none'

通过使用'none'关键词,不管原来如何设定,这些特性在所有浏览的上下文中都会被禁用。

规范

Specification Status Comment
{{SpecName('Feature Policy','#feature-policy-http-header-field','Feature-Policy')}} {{Spec2('Feature Policy')}} Initial definition.

浏览器兼容

{{Compat("http.headers.Feature-Policy")}}

参见