最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I get the hostname where the service worker is installed? - Stack Overflow

programmeradmin3浏览0评论

Usually in javascript you can find the hostname using window.location.hostname, but it seems in service workers you cannot find the hostname using this method.

Is there any other elegant way to get the hostname dynamically?

Usually in javascript you can find the hostname using window.location.hostname, but it seems in service workers you cannot find the hostname using this method.

Is there any other elegant way to get the hostname dynamically?

Share Improve this question asked Jul 25, 2016 at 13:22 ShashankShashank 8741 gold badge9 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 23

Code running in the context of a service worker can get the relevant hostname via self.location.hostname.

If you'd like to explore more of what's available inside of a service worker, the relevant documentation is for the ServiceWorkerGlobalScope.

Specifically, you'll see that the ServiceWorkerGlobalScope inherits from WorkerGlobalScope, and that, in turn, exposes a read-only location property.

发布评论

评论列表(0)

  1. 暂无评论