Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

JavaScript - get url params that are encoded URIs

How would I get the params in a URL that are encoded. Eg, in this URL, I want the 2 cf= and the 3 cat= values:

search.php?st=advanced&qall=&qany=&cat%5B%5D=13&cat%5B%5D=14&cat%5B%5D=11&cf%5B%5D=16&cf%5B%5D=22

I tried this and get null:

const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
console.log(urlParams.get(`cat%5B%5D`)); // would like array [13,14,11]
console.log(urlParams.get(`cf%5B%5D`)); // would like array [16,22]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just use the other method of URLSearchParams object - wich is getAll:

console.log(urlParams.getAll(`cat%5B%5D`)); // will give you an array

you will get exactly what you wanted.

[13,14,11]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...