6

A question asked without possibly an end is how do I get variable from a URL. In all my searching I have found several very good ways to get A=aValue from the url.

But my question is I need

?Company=FLHS&Device=Crosstown_PCC_01&A=aValue&A=secondAValue

I need an array of the two A's in the url and I need to know that aValue was the first one and secondAValue was the second

I have jquery Mobile.

Update

So this is what I have now

var urlParamDevice = getURLParameter('DeviceID');
function getURLParameter(name) {
 return decodeURI(
 (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]
 );

The getURLParameter(name) needs to be a little more robust.

Update 2, 2014年03月07日 Here is what I came up with from the suggested answer

function getQueryParams(name) {
 qs = location.search;
 var params = [];
 var tokens;
 var re = /[?&]?([^=]+)=([^&]*)/g;
 while (tokens = re.exec(qs))
 { 
 if (decodeURIComponent(tokens[1]) == name)
 params.push(decodeURIComponent(tokens[2]));
 }
 return params;
}
asked Mar 5, 2014 at 21:08
2
  • 1
    There are examples of passing arrays (or any other object) as a parameter here: api.jquery.com/jQuery.param Commented Mar 5, 2014 at 21:12
  • Thanks, trying to get an array from the URL not pass one. Commented Mar 5, 2014 at 21:45

3 Answers 3

2

function getparamNameMultiValues(paramName){
	 var sURL = window.document.URL.toString();
	 var value =[];
	 if (sURL.indexOf("?") > 0){
	 var arrParams = sURL.split("?");
	 var arrURLParams = arrParams[1].split("&");
	 for (var i = 0; i<arrURLParams.length; i++){
	 var sParam = arrURLParams[i].split("=");
	 console.log(sParam);
	 if(sParam){
		 if(sParam[0] == paramName){
		 	if(sParam.length>0){
		 		value.push(sParam[1].trim());
		 	}
		 }
	 }
	 }
	 }
	 return value.toString();
	}

answered May 29, 2017 at 7:40

1 Comment

Welcome to SO, posting a code as an answer might not be very useful without explanation. Could you edit your answer to had some explanation? You might be interested to read how to write a good answer
0

That would be by doing as: https://stackoverflow.com/a/1099670/1020854 - But with a little modification

function getQueryParams(qs) {
 qs = qs.split("+").join(" ");
 var params = [], tokens,
 re = /[?&]?([^=]+)=([^&]*)/g;
 while (tokens = re.exec(qs)) {
 params.push({k:decodeURIComponent(tokens[1]),v:decodeURIComponent(tokens[2])});
 }
 return params;
}
var obj = 
 getQueryParams("?Company=FLHS&Device=Crosstown_PCC_01&A=aValue&A=secondAValue")
obj[0] is {k: "Company", v: "FLHS"}
obj[1] is {k: "Device", v: "Crosstown_PCC_01"}
obj[2] is {k: "A", v: "aValue"}
obj[3] is {k: "A", v: "secondAValue"}
answered Mar 5, 2014 at 21:18

3 Comments

Close. I think I can make it work. I only want the "A"
I was not entierly sure what data you where after, but this does indeed extract all the "A"'s
And she only wants the D, heh jokes aside :P
0

Sometimes this will work:

A[]=aValue&A[]=secondAValue

Otherwise you can do this:

A_1=aValue&A_2secondAValue

You can split on the the underscore or use mostly any character of your choice to differentiate values of querystring "A"

answered Mar 5, 2014 at 21:21

2 Comments

I understand, but that is not how the information is coming. I have to consume, not create. It's possible that we have 20 or 30 A's
If you have the whole querystring, I suppose you could split on '&' and then on '=' and build a multimap/hashmap or list of arrays based on the keyname

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.