简体   繁体   中英

How do I get multiple comma separated values from URL

I have a URL like:

http://www.mysite.com/index.html?x=x1&x=x2&x=x3

How do I got the values like below, using JavaScript or JQuery:

var x='x1,x2,x3'
var url = "http://www.mysite.com/index.html?x=x1&x=x2&x=x3";
var params = url.match(/\?(.*)$/)[1].split('&');
var values = [];
for(var i=0; i<params.length; i++){
    values.push( params[i].match(/=(.*)$/)[1] );
}
var result = values.join(","); // "x1,x2,x3"

EDIT : Here is a better solution that lets you select the parameter you want. This is something that I have found buried inside one of my projects, and I didn't write every part of it.

function $_GET(param) {
    var query = window.location.search.substring(1);
    var vars = query.split('&');
    var values = [];
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split('=');
        if (urldecode(pair[0]) == param) {
            values.push(urldecode(pair[1]));
        }
    }
    return values.join(",");
}

// Decode URL with the '+' character as a space
function urldecode(url) {
  return decodeURIComponent(url.replace(/\+/g, ' '));
}

If you directly hit url you can use it as

var fieldValue = ['x1','x2','x3'];
var searchValue = 'x='+ fieldValue.join(',');
window.location.search = searchValue;

This will hit current url to search data for given parameters.

If you want to manually create url then hit search then

var url = "http://www.mysite.com/index.html"; 
window.location.href = url;
var fieldValue = ['x1','x2','x3'];
var searchValue = 'x='+ fieldValue.join(',');
window.location.search = searchValue;

Now you can search values, as per requirement.

I think what you need is PURL. Please refer https://github.com/allmarkedup/purl for detailed usage and guidelines

function GetUrlValue(VarSearch){
var SearchString = window.location.search.substring(1);
var VariableArray = SearchString.split('&');
for(var i = 0; i < VariableArray.length; i++){
    var KeyValuePair = VariableArray[i].split('=');
    if(KeyValuePair[0] == VarSearch){
        return KeyValuePair[1];
    }
}

}

read here http://javascriptproductivity.blogspot.in/2013/02/get-url-variables-with-javascript.html

You can easily find query string in jquery using jquery split

Try this function to get Query String as a array object:

function getUrlVars()
{
    var vars = [];
    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[1]);             
    }
    return vars;
}

The function returns an array/object with your URL parameters and their values. So, you can use jquery .join() to convert it into comma separated values:

var result = vars.join(",");

Try in jsfiddle

Maybe use Regex:

var s = window.location.search;
var foo = s.match(/x=([0-9a-zA-Z]+)/g).join(",").replace(/x=/g, ""); // x1,x2,x3

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