files/en-us/web/api/windowsharedstorage/selecturl/index.md
{{APIRef("Shared Storage API")}}{{deprecated_header}}
The selectURL() method of the
{{domxref("WindowSharedStorage")}} interface executes a URL Selection operation that is registered in a module added to the current origin's {{domxref("SharedStorageWorklet")}}.
[!NOTE] The URL Selection output gate is used to select a URL from a provided list to display to the user, based on shared storage data.
selectURL(name, urls)
selectURL(name, urls, options)
name
urls
url
reportingMetadata {{optional_inline}}
"click" : "my-reports/report1.html". The URLs act as destinations for reports submitted with a destination of type "shared-storage-select-url", typically submitted via a {{domxref("Fence.reportEvent()")}} or {{domxref("Fence.setReportEventDataForAutomaticBeacons()")}} method call.options {{optional_inline}}
data {{optional_inline}}
keepAlive {{optional_inline}}
true, the {{domxref("SharedStorageWorkletGlobalScope")}} of the associated worklet is kept alive, and the operation can be run again. Therefore, you need to set keepAlive to true for each operation that is not intended to be the last one. The default value, false, means that the {{domxref("SharedStorageWorkletGlobalScope")}} is terminated after the operation is run and cannot be run again.resolveToConfig {{optional_inline}}
true, the fulfillment value of the {{jsxref("Promise")}} returned by run() will be a {{domxref("FencedFrameConfig")}} object that can be used to load content into a {{htmlelement("fencedframe")}} via its config attribute. The default value, false, means that the fulfillment value will be a URL that can be used to load content into an {{htmlelement("iframe")}}.A {{jsxref("Promise")}} that fulfills with a {{domxref("FencedFrameConfig")}} object or a string representing a URL, depending on the value of the resolveToConfig option.
urls is empty or exceeds the maximum allowed length (which is browser-specific).url property contains an invalid URL.// 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}}
{{Compat}}