2017-10-18 130 views

回答

4

.search财产,我并不清楚是否你问有关获取服务工作者脚本的URL,或在服务工作者范围下打开的所有客户端页面的URL。所以......这里是如何做到既:

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

// 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 property

0

你可以得到waiting.scriptURLactive.scriptURL,通过结果来URL()构造,获取对象

navigator.serviceWorker.register("sw.js?abc=123") 
    .then(function(reg) { 
    const scriptURL = reg.waiting && reg.waiting.scriptURL || reg.active.scriptURL; 
    const url = new URL(scriptURL); 
    const queryString = url.search; 
    console.log(queryString); 
    }).catch(function(err) { 
    console.log("err", err); 
    });