showing results for - "javascript create url with parameters"
Claudia
31 Jun 2017
1function getAllUrlParams(url) {
2
3  // get query string from url (optional) or window
4  var queryString = url ? url.split('?')[1] : window.location.search.slice(1);
5
6  // we'll store the parameters here
7  var obj = {};
8
9  // if query string exists
10  if (queryString) {
11
12    // stuff after # is not part of query string, so get rid of it
13    queryString = queryString.split('#')[0];
14
15    // split our query string into its component parts
16    var arr = queryString.split('&');
17
18    for (var i = 0; i < arr.length; i++) {
19      // separate the keys and the values
20      var a = arr[i].split('=');
21
22      // set parameter name and value (use 'true' if empty)
23      var paramName = a[0];
24      var paramValue = typeof (a[1]) === 'undefined' ? true : a[1];
25
26      // (optional) keep case consistent
27      paramName = paramName.toLowerCase();
28      if (typeof paramValue === 'string') paramValue = paramValue.toLowerCase();
29
30      // if the paramName ends with square brackets, e.g. colors[] or colors[2]
31      if (paramName.match(/\[(\d+)?\]$/)) {
32
33        // create key if it doesn't exist
34        var key = paramName.replace(/\[(\d+)?\]/, '');
35        if (!obj[key]) obj[key] = [];
36
37        // if it's an indexed array e.g. colors[2]
38        if (paramName.match(/\[\d+\]$/)) {
39          // get the index value and add the entry at the appropriate position
40          var index = /\[(\d+)\]/.exec(paramName)[1];
41          obj[key][index] = paramValue;
42        } else {
43          // otherwise add the value to the end of the array
44          obj[key].push(paramValue);
45        }
46      } else {
47        // we're dealing with a string
48        if (!obj[paramName]) {
49          // if it doesn't exist, create property
50          obj[paramName] = paramValue;
51        } else if (obj[paramName] && typeof obj[paramName] === 'string'){
52          // if property does exist and it's a string, convert it to an array
53          obj[paramName] = [obj[paramName]];
54          obj[paramName].push(paramValue);
55        } else {
56          // otherwise add the property
57          obj[paramName].push(paramValue);
58        }
59      }
60    }
61  }
62
63  return obj;
64}
65
Paola
21 Oct 2018
1// URL w/ PARAMETERS
2let origin = "https://www.google.com"
3let pathname =  "/search"
4let query = "?q=javascript%20create%20url%20with%20parameters"
5
6let url = origin + pathname + query
7
8console.log(url);
queries leading to this page
read params from url in jsget parameters from url javascriptjavascript reference in url paramtercreate url params jsjs get parameters from urlhow to javascript function in url parameterjavascript current url with parametersjs url parachange parameter in url javascriptpassing parameters to url in javascripthow to pass param in urljavascript get url paramhtml parameters in urlurl with parameters javascripthtml read url params javascripthow to parse url parameters in javascriptpass url parameter to script in jsstring to url javascript and get parametersget parameter from url javascripthow to read url parameters in javascriptset value to the value of url parameterjavascript url parameters by 23create url with paramshow to add parameters in url using javascriptcheck for url parameters javascripthtml javascript url parametersjavascript get parameters from urlchange parameter url javascriptget a url parameter with javascriptjavascript goto url with parametersjs get url parametercreate url with parameters javascriptjavascript build url with parametersget url parameters jswhat is url parameters in jsjavascript get url parameter w3schoolsurl params javascriptjavascript creat url parametersjavascript read parameter from urljavascript text url parametersget params from url javascripthow to include parameters in a js urlhow to set url parameters in javascripthow to set a param from url in jsget param from url in jsget url parameters javascripturl params jsurl parameters jsparameters from url javascriptjavascript url argumentsjavascript get parameter from urlhow to change url param in javascriptget parameter from url in javascripthow to get url params in javascriptjavascript url parametersjavascript url paramshow to add parameters to url in javascripturl parameters in hsget url params jsstore url params in a functionjavascript get url parametersjavascript create url with parametershow to pass value in url javascriptpass url parameter to javascripturl parameter javascriptpassing url parameters in javascripturl param jsjavascript set url parametersgetting url parameters in javascripthow to pass parameter in url in javascriptget url parameter javascripthow to access url parameters in javascriptget params url javascriptjavascript get url params from stringget url params with jsjs read url parametersjavascript url with parametershow to pass url parameters in javascriptjs params from urljavascript code to read url parameters and append the urlhow to use url parameters javascriptjavascript update url parameterjavascript read url parametersjavascript change url parameterhow to get url parameters in javascriptparams url javascriptset url parameter toread url parameters javascriptread params from url in javascriptsimple html dom pass url variable as parameter findhow to get url parameter values in jsjs set url parameterget url params in jshow to get parameter value from url in javascriptget parameter from url jsjavascript get url parameterhow to get url parameter in javascriptset parameter from url stringjs url parametersset parameter to urlhow to read parameters from url in javascriptgetting url parameters with jsjs url paramset url parameters javascriptjs get url parametersget params from url jshow to get the url params in javascriptcreate url parameters in javascriptjavascript variable parameters urljs url parameterjs url paramsjavascript parse url parametershow to retrieve parameters from the url in javascriptjavascript url set parametersjavascript convert string to url parameterhow to retrieve get url parameters in javascripthow to add url parameters in javascriptjavascript add url parameter to current urlset parameters in url javascripthow to pass parameter to url in javascriptget url params javascripturl parameters javascriptjs creat url with paramshow to read a url parameters in javascriptjs build url with parametershow to put a parameter in url in javascriptchecking url parameters using javascriptjavascript send parameters to urlbuild url with parameters jsparse url parameters javascriptget url parameter jsjavascript set url parameterurl set paramsget url params in javascripturl script with parametersjavascript string to url parameterhow to get url parameter value in javascriptjs construct url with paramsjavascript create url with parameters