10 examples of 'parse url parameters javascript' in JavaScript

Every line of 'parse url parameters javascript' 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
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}
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

9function parseQueryString(url) {
10 var str = url.split("?")[1];
11 var items = str.split("&");
12 var result = {};
13 var arr = [];
14
15 for (var i = 0; i < items.length; i++) {
16 if (items[i]) {
17 arr = items[i].split('=');
18 if (arr.length <= 1) {
19 continue;
20 }
21 //decode 参数值,避免ajax请求参数被自动二次编码
22 result[arr[0]] = decodeURIComponent(arr[1]);
23 }
24 }
25
26 return result;
27}
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}
38function parseUrlParams(url) {
39 // Get settings from url, 'hash' takes precedence over 'search' component
40 // don't use document.location.hash due to browser differences.
41 var query;
42 var queryIdx = url.indexOf('?');
43 var hashIdx = url.indexOf('#');
44 if (hashIdx === -1) {
45 query = url.substr(queryIdx + 1);
46 } else {
47 // essentially replaces "#" with "&"
48 query = [url.substr(queryIdx + 1, hashIdx - queryIdx - 1), '&',
49 url.substr(hashIdx + 1)].join('');
50 }
51 return query.split('&');
52}
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}
192function parseParamsFromUrl() {
193 var params = {};
194 var parts = window.location.search.substr(1).split('&');
195 for (var i = 0; i < parts.length; i++) {
196 var keyValuePair = parts[i].split('=');
197 var key = keyValuePair[0];
198 params[key] = keyValuePair[1];
199 }
200 return params;
201} // function parseParamsFromUrl()
646function processParams() {
647 var params = {};
648 var q = document.URL.split('?')[1];
649 if(q != undefined){
650 q = q.split('&');
651 for(var i = 0; i < q.length; i++){
652 var pv = q[i].split('=');
653 var p = pv[0];
654 var v = pv[1];
655 params[p] = urldecode(v);
656 }
657 }
658}
72export function parseQuery(url){
73 var urlParts = url.split('?');
74 var data = {};
75
76 if (urlParts.length > 1) {
77 var query = urlParts[1];
78 // Get rid of any anchor that might be on the end of the query:
79 query = query.split('#')[0];
80 query.split('&').forEach((nameVal) => {
81 var parts = nameVal.split('=');
82 data[parts[0]] = decodeURIComponent(parts[1] || '');
83 });
84 }
85
86 return data;
87}
13function parseUrl(query) {
14 var index = query.indexOf('?');
15 if (index >= 0)
16 query = query.substring(index + 1);
17 var res = {};
18 var vars = query.split("&");
19 for (var i = 0; i < vars.length; i++) {
20 var indexPos = vars[i].indexOf("=");
21 if (indexPos < 0)
22 continue;
23
24 var key = vars[i].substring(0, indexPos);
25 var value = vars[i].substring(indexPos + 1);
26 value = decodeURIComponent(value.replace(/\+/g, ' '));
27 mergeParamIntoObject(res, key, value);
28 }
29 return res;
30}
32function getURLArgs(_string) {
33 var args = {};
34 var pairs = _string.split("&");
35 for(var i = 0; i < pairs.length; i++) {
36 var pos = pairs[i].indexOf('=');
37 if (pos == -1) continue;
38 var argname = pairs[i].substring(0,pos);
39 var value = pairs[i].substring(pos+1);
40 args[argname] = unescape(value);
41 }
42 return args;
43}

Related snippets