Open sollrei opened 4 years ago
示例地址 https://example.com/?product=shirt&color=blue&newuser&size=m
https://example.com/?product=shirt&color=blue&newuser&size=m
const queryString = window.location.search; // ?product=shirt&color=blue&newuser&size=m const urlParams = new URLSearchParams(queryString);
URLSearchParams IE不支持
const product = urlParams.get('product') console.log(product); // "shirt" const newUser = urlParams.get('newuser') console.log(newUser); // "" console.log(urlParams.has('product')); // true console.log(urlParams.has('paymentmethod')); // false console.log(urlParams.getAll('size')); // [ 'm' ] for (const key of urlParams.keys()) console.log(key); // product, color, newuser, size
参考: https://www.sitepoint.com/get-url-parameters-with-javascript/
示例地址
https://example.com/?product=shirt&color=blue&newuser&size=m