這個問題在這里已經有了答案: 從“GET”引數中獲取值(JavaScript) (63 個回答) 6 小時前關閉。
我有一個類似于https://app.ticketmaster.com/discovery/v2/events?apikey=xxxxxx&keyword=Sarah Jarosz&latlong=xx.xxxxx,-xxx.xxxx&radius=15&unit=miles&locale= *的鏈接,它作為我的函式中的引數。我想獲取此鏈接的關鍵字部分的值,并且希望找到比使用子字串撰寫自己的函式更容易的方法。
我熟悉獲取鏈接部分的 window.locaiton.____ 方法,但這是作為函式中的引數傳遞的,因此我無法使用該方法。我嘗試使用此問題中定義的方法Parse an URL in JavaScript但我不能使用 .keyword 而不是 .search 來僅獲取關鍵字部分。
uj5u.com熱心網友回復:
使用 URL 和 URLSearchParams api
const urlString =' https://app.ticketmaster.com/discovery/v2/events?apikey=xxxxxx&keyword=Sarah Jarosz&latlong=xx.xxxxx,-xxx.xxxx&radius=15&unit=miles&locale='
function getKeyword (str) {
const url = new URL(str);
return url.searchParams.get('keyword');
}
console.log(getKeyword(urlString))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/366132.html
標籤:javascript 网址
