简体   繁体   中英

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

  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?

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']);

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"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM