大网数据广告活动
Web 标准与前端开发

WindowSharedStorage

· 52CE 我爱测

The **`WindowSharedStorage`** interface of the Shared Storage API represents the shared storage for a particular origin within a standard browsing context.

Shared Storage API

The WindowSharedStorage interface of the Shared Storage API represents the shared storage for a particular origin within a standard browsing context.

WindowSharedStorage is accessed via Window.sharedStorage.

Instance properties

  • WindowSharedStorage.worklet
    • : Contains the SharedStorageWorklet instance representing the shared storage worklet for the current origin. SharedStorageWorklet includes the Worklet.addModule method, which is used to add a module to the shared storage worklet.

Instance methods

WindowSharedStorage inherits properties from its parent interface, SharedStorage.

  • WindowSharedStorage.run
    • : Executes a Run output gate operation that has been registered in a module added to the SharedStorageWorklet of the current origin.
  • WindowSharedStorage.selectURL
    • : Executes a URL Selection output gate operation that has been registered in a module added to the SharedStorageWorklet of the current origin.

Examples

// Randomly assigns a user to a group 0 or 1
function getExperimentGroup() {
  return Math.round(Math.random());
}

async function injectContent() {
  // Add the module to the shared storage worklet
  await window.sharedStorage.worklet.addModule("ab-testing-worklet.js");

  // Assign user to a random group (0 or 1) and store it in shared storage
  window.sharedStorage.set("ab-testing-group", getExperimentGroup(), {
    ignoreIfPresent: true,
  });

  // Run the URL selection operation
  const fencedFrameConfig = await window.sharedStorage.selectURL(
    "ab-testing",
    [
      { url: `https://your-server.example/content/default-content.html` },
      { url: `https://your-server.example/content/experiment-content-a.html` },
    ],
    {
      resolveToConfig: true,
    },
  );

  // Render the chosen URL into a fenced frame
  document.getElementById("content-slot").config = fencedFrameConfig;
}

injectContent();

See the Shared Storage API landing page for a walkthrough of this example and links to other examples.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。