10 examples of 'how to get query string value in javascript' in JavaScript

Every line of 'how to get query string value in 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
230function updateQueryString (key, value) {
231 value = encodeURIComponent(value)
232
233 var url = window.location.href.replace(/([?&])(?:q|h)=([^&]+)(&|$)/, function (all, pre, value, end) {
234 if (end === '&') {
235 return pre
236 }
237 return ''
238 })
239 var re = new RegExp('([?&])' + key + '=.*?(&|#|$)(.*)', 'gi')
240 var hash
241
242 if (re.test(url)) {
243 if (typeof value !== 'undefined' && value !== null) { return url.replace(re, '$1' + key + '=' + value + '$2$3') } else {
244 hash = url.split('#')
245 url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '')
246 if (typeof hash[1] !== 'undefined' && hash[1] !== null) { url += '#' + hash[1] }
247 return url
248 }
249 } else {
250 if (typeof value !== 'undefined' && value !== null) {
251 var separator = url.indexOf('?') !== -1 ? '&' : '?'
252 hash = url.split('#')
253 url = hash[0] + separator + key + '=' + value
254 if (typeof hash[1] !== 'undefined' && hash[1] !== null) { url += '#' + hash[1] }
255 return url
256 } else { return url }
257 }
258}
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

82function getQueryStringValue(key) {
83 return decodeURIComponent(
84 window.location.search.replace(
85 new RegExp(
86 "^(?:.*[&\\?]" +
87 encodeURIComponent(key).replace(/[\.\+\*]/g, "\\$&") +
88 "(?:\\=([^&]*))?)?.*$",
89 "i"
90 ),
91 "$1"
92 )
93 );
94}
100export function objectToQueryString(value: JSONObject): string {
101 const keys = Object.keys(value).filter(key => key.length > 0);
102
103 if (!keys.length) {
104 return '';
105 }
106
107 return (
108 '?' +
109 keys
110 .map(key => {
111 const content = encodeURIComponent(String(value[key]));
112
113 return key + (content ? '=' + content : '');
114 })
115 .join('&')
116 );
117}
738public getQueryText(): string {
739 const filter = this.filters._query;
740 return filter instanceof Filter
741 ? filter.getValues()[0]
742 : "";
743}
58export function getQueryValue(name, url) {
59 if (!url) url = window.location.href;
60 name = name.replace(/[\[\]]/g, "\\$&");
61 const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
62 const results = regex.exec(url);
63 if (!results) return null;
64 if (!results[2]) return '';
65 return decodeURIComponent(results[2].replace(/\+/g, " "));
66}
334function encodeQueryValue(key, value) {
335 return encodeURIComponent(key) +
336 "=" +
337 encodeURIComponent(typeof value === "object" ?
338 JSON.stringify(value) :
339 "" + value);
340}
2function getQuery(querystring) {
3 var query = {};
4
5 var pairs = querystring.split('&'),
6 length = pairs.length,
7 keyval = [],
8 i = 0;
9
10 for (; i < length; i++) {
11 keyval = pairs[i].split('=', 2);
12 try {
13 keyval[0] = decodeURIComponent(keyval[0]); // key
14 keyval[1] = decodeURIComponent(keyval[1]); // value
15 } catch (e) {}
16
17 if (query[keyval[0]] === undefined) {
18 query[keyval[0]] = keyval[1];
19 } else {
20 query[keyval[0]] += ',' + keyval[1];
21 }
22 }
23
24 return query;
25}
3function GetUrlValue (VarSearch) {
4 var SearchString = window.location.search.substring(1)
5 var VariableArray = SearchString.split('&')
6
7 for (var i = 0; i < VariableArray.length; i++) {
8 var KeyValuePair = VariableArray[i].split('=')
9
10 if (KeyValuePair[0] === VarSearch) {
11 return KeyValuePair[1]
12 }
13 }
14}
8function createQueryString(query) {
9 const segments = [];
10
11 for (const key of Object.keys(query)) {
12 segments.push(`${key}=${query[key]}`);
13 }
14
15 return segments.join('&');
16}
22export function _str__query(query = {}) {
23 const a1__str__query = []
24 for (let key in query) {
25 const value = query[key]
26 const value__ = value == null ? '' : value
27 a1__str__query.push(`${encodeURIComponent(key)}=${encodeURIComponent(value__)}`)
28 }
29 return a1__str__query.length ? `?${a1__str__query.join('&')}` : ''
30}

Related snippets