2017-10-18 136 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); 
    });