как извлечь параметр из url с помощью jquery и javascript
Ответы

Ivan Mamtsev
20 декабря 2024
Для извлечения url лучше все же пользоваться современными абстракциями JS, как UrlSearchParams
. Но для подджерки старых браузеров можно использовать jQuery
function getParameterWithJQuery(paramName) {
$.urlParam = function(name) {
// Создаём регулярное выражение для поиска параметра
const results = new RegExp('[?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results === null) {
return null;
}
return decodeURIComponent(results[1]) || null;
};
return $.urlParam(paramName);
}
// Для URL: https://example.com?user=Alice&role=admin
const user = getParameterWithJQuery('user'); // => "Alice"
const role = getParameterWithJQuery('role'); // => "admin"
0
0