Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get multiple parameters with same name from a URL in JavaScript

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;
}
like image 902
Brian Hanf Avatar asked Mar 05 '14 21:03

Brian Hanf


People also ask

How do I get multiple parameters from URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

Can you use Javascript to get URL parameter values?

The short answer is yes Javascript can parse URL parameter values. You can do this by leveraging URL Parameters to: Pass values from one page to another using the Javascript Get Method. Pass custom values to Google Analytics using the Google Tag Manager URL Variable which works the same as using a Javascript function.

How does rest URL handle multiple query parameters?

@PUT @Path("{user}/{directory:. +}") public Response doshare(@PathParam("user")String name, @PathParam("directory")String dir, @QueryParam("name")String sharename, @QueryParam("type")String type){ mongoDAOImpl impl=new mongoDAOImpl(); Mongo mongo=impl. getConnection("127.0. 0.1","27017"); DB db=impl.


1 Answers

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();
	}
like image 59
Sourav Barman Avatar answered Oct 02 '22 15:10

Sourav Barman