4

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'
3
  • Is it valid to have multiple query parameters with the same key ? Commented Nov 5, 2013 at 7:42
  • 1
    This is valid when we submit form with multiple checkbox values. Commented Nov 5, 2013 at 7:45
  • You're right, stupid comment from me! Commented Nov 5, 2013 at 7:48

6 Answers 6

2
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, ' '));
}
Sign up to request clarification or add additional context in comments.

1 Comment

This my mock URL. How I got URL and get result by pass value param name x.
2

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.

Comments

0

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

Comments

0
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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.