เข้าถึง Query String ใน URL ด้วย URLSearchParams: get() method

ตัวอย่าง URL ของหน้าเว็บที่ต้องการดึงข้อมูล GET

https://example.com/?name=Jonathan&age=18


ดึงด้วย JS จะได้ข้อมูลดังนี้

JS

let params = new URLSearchParams(document.location.search);

let name = params.get("name"); // is the string "Jonathan"

let age = parseInt(params.get("age"), 10); // is the number 18


Browser compatibility

Report problems with this compatibility data on GitHub


ที่มา https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/get

ความคิดเห็น