10 examples of 'js get params from url' in JavaScript

Every line of 'js get params from url' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
179export function getURLParams(url: string): {[key: string]: string} {
180 if (!url) {
181 return {};
182 }
183
184 let queryString = url.indexOf('?') !== -1 ? url.split('?')[1] : url;
185 if (queryString.indexOf('#')) {
186 queryString = queryString.split('#')[0];
187 }
188
189 const queryEntries = queryString.split('&');
190 let queryParams: {[key: string]: string} = {};
191 for (let i = 0; i < queryEntries.length; i++) {
192 let queryEntryComponents = queryEntries[i].split('=');
193 queryParams[queryEntryComponents[0].toLowerCase()] =
194 decodeURIComponent(queryEntryComponents[1]);
195 }
196 return queryParams;
197}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

49function getUrlParameters(url) {
50 url = url || window.location.search;
51
52 const query = url.substring(url.indexOf('?') + 1);
53 const search = /([^&=]+)=?([^&]*)/g;
54 const result = [];
55 let match;
56
57 while ((match = search.exec(query)) !== null) { // eslint-disable-line no-cond-assign
58 result.push([decodeQueryComponent(match[1]), decodeQueryComponent(match[2])]);
59 }
60
61 return result;
62}
1function getUrlParams () {
2 var match;
3 var pl = /\+/g; // Regex for replacing addition symbol with a space
4 var search = /([^&=]+)=?([^&]*)/g;
5 var decode = function (s) { return decodeURIComponent(s.replace(pl, ' ')); };
6 var query = window.location.search.substring(1);
7 var urlParams = {};
8
9 match = search.exec(query);
10 while (match) {
11 urlParams[decode(match[1])] = decode(match[2]);
12 match = search.exec(query);
13 }
14 return urlParams;
15}
10function getQueryStringParams(url) {
11 var params = {};
12 if (!url ) {
13 return params;
14 }
15
16 var pairs = url.replace(/^[^\?]+\??/,'').split(/[;&]/);
17 for (var i = 0; i < pairs.length; i++) {
18 var keyVal = pairs[i].split('=');
19 if (!keyVal || keyVal.length != 2) continue;
20
21 var key = decodeURIComponent(keyVal[0]);
22 var val = decodeURIComponent(keyVal[1]);
23
24 val = val.replace(/\+/g, ' ');
25 params[key] = val;
26 }
27
28 return params;
29}
86function get_url_query(url)
87{
88 url = url+'';
89 var idx = url.indexOf('?'),
90 idx2 = url.indexOf('#');
91 return idx == -1 ? '' :
92 (idx2 == -1 ? url.substr(idx + 1) : url.substring(idx + 1, idx2));
93}
69function getAllParams(url) {
70 var vars = [], hash, obj;
71 var hashes = url.slice(url.indexOf('?') + 1).split('&');
72 for (var i = 0; i < hashes.length; i++) {
73 hash = hashes[i].split('=');
74 vars.push(hash);
75 }
76 return vars;
77}
118function extractGETParameters(url) {
119 return url.split(/\?(.*)?$/).slice(1).join('');
120}
35export function getUrlParam (location: string, key: string) {
36 const url = new URL(location);
37 return url.searchParams.get(key);
38}
51function getUrlParam(s) {
52 var dRet = {}, b = s.split('&');
53 b.forEach( function(item) {
54 if (!item) return;
55 var b2 = item.split('='), sName = b2[0].trim();
56 if (sName)
57 dRet[sName] = (b2[1] || '').trim();
58 });
59 return dRet;
60}
3function getParam(s) {
4 const url = new window.URL(window.location.href)
5 return url.searchParams.get(s)
6}

Related snippets