Samun duk ƙimar kirtani daga URL tare da Javascript

Samun Sigar URL

Zaton URL shine: http://bfotool.com?size=L&color=red&price=10

var queryString = window.location.search;  
console.log(queryString);  
// ?size=L&color=red&price=10

Yi amfani da URLSearchParams don tantancewa

var urlParams = new URLSearchParams(queryString);

Kira kowane hanyoyin sa akan sakamakon.

var size= urlParams.get('size')  
console.log(size);  
// L  
  
var color = urlParams.get('color')  
console.log(color);  
// red  
  
var price= urlParams.get('price')  
console.log(price);  
// 10

Duban Kasancewar Ma'auni

yi amfani da  URLSearchParams.has()
console.log(urlParams.has('color'));  
// true  
  
console.log(urlParams.has('keyword'));  
// false

Sami duk Ma'auni

var queryString = window.location.search;  
  
var urlParams = new URLSearchParams(queryString);  
  
var keys = urlParams.keys(),  
    values = urlParams.values(),  
    entries = urlParams.entries();  
  
for(const key of keys) {  
    console.log(key);  
}  
// size, color, price  
  
for(const value of values) {  
    console.log(value);  
}  
// L, red, 10  
  
for(const entry of entries) {  
    console.log(`${entry[0]}: ${entry[1]}`);  
}  
// site: L  
// color: red  
// price: 10