How to get parameters name and value from url using jquery?

我的梦境 提交于 2019-12-10 23:56:49

问题


  function getURLParameter(url, name) {
        return (RegExp(name + '=' + '(.+?)(&|$)').exec(url)||[,null])[1];
  }

This function returns parameter's value receives url and parameter's name.

I want to change this function returns parameter's all name and value receives url.

How can I modify?


回答1:


Hope this helps

function getURLParams(url) {
  let params = {};
  new URLSearchParams(url.replace(/^.*?\?/, '?')).forEach(function(value, key) {
    params[key] = value
  });
  return params;
}

var params = getURLParams('https://www.google.com/search?q=query+string&oq=query+string&aqs=chrome..69i57j69i60l2j0l3.10413j0j7&sourceid=chrome&ie=UTF-8');
console.log(params);
console.log(params['ie']);



回答2:


function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

the to get your required value use

getUrlVars()["YourParameterInQueryString"]


来源:https://stackoverflow.com/questions/55057130/how-to-get-parameters-name-and-value-from-url-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!