Вот несколько методов работы со строками запроса на разных языках программирования:
-
JavaScript:
// Get the current URL var url = window.location.href; // Get the value of a specific parameter var paramValue = new URLSearchParams(window.location.search).get('parameterName'); // Add or update a parameter var newUrl = new URL(url); newUrl.searchParams.set('parameterName', 'parameterValue'); var updatedUrl = newUrl.toString(); -
Python:
from urllib.parse import urlparse, parse_qs, urlencode, urlunparse # Get the current URL url = 'https://example.com/path/to/page?name=ferret&color=purple' # Get the value of a specific parameter parameters = parse_qs(urlparse(url).query) paramValue = parameters['parameterName'][0] # Add or update a parameter parameters['parameterName'] = ['parameterValue'] newQuery = urlencode(parameters, doseq=True) newUrl = urlunparse(urlparse(url)._replace(query=newQuery)) -
PHP:
// Get the current URL $url = 'https://example.com/path/to/page?name=ferret&color=purple'; // Get the value of a specific parameter $parameters = []; parse_str(parse_url($url, PHP_URL_QUERY), $parameters); $paramValue = $parameters['parameterName']; // Add or update a parameter $parameters['parameterName'] = 'parameterValue'; $newQuery = http_build_query($parameters); $newUrl = parse_url($url, PHP_URL_SCHEME) . '://' . parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH) . '?' . $newQuery;