从 Service Worker 获取页面 URL 参数 [英] Get page URL parameters from a service worker

查看:82
本文介绍了从 Service Worker 获取页面 URL 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Service Worker 获取带有参数的页面 URL?

How do I get page URL with parameters from a service worker?

我尝试过 self.registration.scope 但不包括参数.

I have tried self.registration.scope but that doesn't include the parameters.

推荐答案

我不清楚您是要获取 Service Worker 脚本的 URL,还是在其下打开的所有客户端页面的 URL服务工作者的范围.所以......这里是如何做到这两点:

I'm not clear as to whether you're asking about getting the service worker script's URL, or the URLs of all of the client pages that are open under the service worker's scope. So... here's how to do both:

// Get a URL object for the service worker script's location.
const swScriptUrl = new URL(self.location);

// Get URL objects for each client's location.
self.clients.matchAll({includeUncontrolled: true}).then(clients => {
  for (const client of clients) {
    const clientUrl = new URL(client.url);
  }
});

在任何一种情况下,一旦您拥有 URL 对象,您就可以使用它的 searchParams 属性 如果您对查询参数感兴趣:

In either of those cases, once you have a URL object, you can use its searchParams property if you're interested in the query parameters:

if (url.searchParams.get('key') === 'value') {
  // Do something if the URL contains key=value as a query parameter.
}

这篇关于从 Service Worker 获取页面 URL 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆