CookieStoreManager:subscribe() 方法
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
备注: 此特性在 Service Worker 中可用。
CookieStoreManager
接口的 subscribe()
方法订阅 ServiceWorkerRegistration
的 cookie 更改事件。
语法
js
subscribe(subscriptions)
参数
subscriptions
-
一个对象列表,每个对象包括以下属性:
返回值
异常
示例
在此示例中,由 registration
表示的 ServiceWorkerRegistration
订阅了 "/path1"
作用域下命名为 "cookie1"
的 cookie 的变更事件。
js
const subscriptions = [{ name: "cookie1", url: `/path1` }];
await registration.cookies.subscribe(subscriptions);
传给 subscribe()
方法的 URL 可能比 Service Worker Registration 的作用域更小。以下示例订阅是针对 /path/one/
的,所以只会接收到第一个 cookie 的变更事件,而第二个收不到。
js
registration.cookies.subscribe([{ name: "cookie1", url: "/path/one/" }]); // 订阅 cookie1
cookieStore.set({ name: "cookie1", value: "cookie-value", path: "/path/one/" }); // 收到一个变更事件
cookieStore.set({ name: "cookie1", value: "cookie-value", path: "/path/two/" }); // 不会受到变更事件
规范
Specification |
---|
Cookie Store API # dom-cookiestoremanager-subscribe |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
subscribe |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.