0

Не могу сделать проверку на наличие параметров в URL, чтобы выполнять действие только при их наличии. Сами параметры получаю вот так.

$.urlParam = function(name) {
  var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
  return results[1] || 0;
}

$.urlParam('param1');
$.urlParam('id');
$.urlParam('param2');

Буду благодарен за помощь.

1 ответ 1

-1

Ныа

const getParam=(name, url=window.location.href)=>{
    name = name.replace(/[\[\]]/g, '\\$&');
    const results = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`).exec(url);
    if(!results || !results[2])return false;
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
};

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.