Modern browsers provide native APIs for manipulating URLs and query strings. These APIs, including URL and URLSearchParams, should be prioritized for compatibility with modern browsers.
Original Solution:
Prior to native APIs, all GET request parameters were accessible through the window.location.search property. However, this requires manual parsing of the query string. The following function can be used:
function getQueryParam(name) {
const regex = new RegExp('[?&]' encodeURIComponent(name) '=([^&]*)');
const result = regex.exec(location.search);
return result ? decodeURIComponent(result[1]) : undefined;
}
This function takes a GET parameter name and returns its value. If the parameter does not exist or has no value, it returns undefined.
Example:
const foo = getQueryParam('foo');
This will assign the value of the GET parameter foo to the foo variable.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3