简体   繁体   中英

Get values of all the same parameter from url in jQuery

I have an url have contain two parameters.

Ex: https://www.google.com.vn/search?q=news&oq=news&aqs=chrome..69i57j69i60l3.299j0j4&sourceid=chrome&es_sm=93&ie=UTF-8#q=bbc

Parameter 1: q=news

Parameter 2:q=bbc

I want to get all value have the same parameter, but I can only get value in first the parameter.

This is my code:

function getParameterByName(name, url) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&#]" + name + "=([^&#]*)"),
    results = regex.exec(url);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

Use $(location).attr('href'); and parse through the parameters yourself

what i have tried is only IDEA of how you can get all paremater.

 var a="https://www.google.com.vn/search?q=news&oq=news&aqs=chrome..69i57j69i60l3.299j0j4&sourceid=chrome&es_sm=93&ie=UTF-8#q=bbc"
 var b= a.split("?")[1]
 b.split("&")

By this technique you will get all parameter.
You need to convert this code to your required code.

The first q is in querystring , the second one is in hash . You should parse them in JavaScript:

QueryString

// the method is taken from http://stackoverflow.com/a/3855394/3971911
var qs = (function(a) {
    if (a == "") return {};
    var b = {};
    for (var i = 0; i < a.length; ++i)
    {
        var p=a[i].split('=');
        if (p.length != 2) continue;
        b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
    }
    return b;
})(window.location.search.substr(1).split('&'));

Hash

// the method is taken from http://stackoverflow.com/a/11920807/3971911
function getHashValue(key) {
  return location.hash.match(new RegExp(key+'=([^&]*)'))[1];
}

Now you can use them like this:

alert(qs['q']); // news
alert(getHashValue('q')); // bbc

try this:

function getParameterByName(name, url,position) {
  url=position==1?url:url.substr(url.lastIndexOf('#'));
  name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  var regex = new RegExp("[\\?&#]" + name + "=([^&#]*)"),
  results = regex.exec(url);
  return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

 var url='https://www.google.com.vn/search?q=news&oq=news&aqs=chrome..69i57j69i60l3.299j0j4&sourceid=chrome&es_sm=93&ie=UTF-8#q=bbc';

alert(getParameterByName('q',url,1));
alert(getParameterByName('q',url,2));

Demo

Other than an exercise in coding, you're probably better off using existing code.

Something like:

Pros:

  • It has tests
  • It's had more useful eyes cast over it (guessing here).
  • The people that have likely contributed to it will probably have used it.
  • If you find an issue with it, those people involved will have a keen interest in solving the problem as they're likely using it.

Cons:

  • None! :D

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