10 examples of 'add query parameter to url javascript' in JavaScript

Every line of 'add query parameter to url 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
154function addURLParam(url, name, value){
155 url += (url.indexOf("?") == -1 ? "?" : "&");
156 url += encodeURIComponent(name) + "=" + encodeURIComponent(value);
157 return url;
158}
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

20function addToQueryString(url, params) {
21 if (!params) return url;
22
23 var keys = Object.keys(params);
24 if (!keys.length) return url;
25
26 var obj = parser.parse(url, true);
27 obj.query = obj.query || {};
28 extend(obj.query, params);
29
30 // Force use of .query property when formatting. See:
31 // http://nodejs.org/api/url.html#url_url_format_urlobj
32 delete obj.search;
33
34 return parser.format(obj);
35}
10function insertParamToURL(key, value)
11{
12 key = escape(key); value = escape(value);
13
14 var kvp = document.location.search.substr(1).split('&');
15
16 var i=kvp.length; var x;
17 while(i--) {
18 x = kvp[i].split('=');
19
20 if (x[0]==key)
21 {
22 x[1] = value;
23 kvp[i] = x.join('=');
24 break;
25 }
26 }
27
28 if(i<0) {kvp[kvp.length] = [key,value].join('=');}
29
30 document.location.search = kvp.join('&');
31}
190export function appendQuery(url, query) {
191 if (query === '') return url;
192 let parts = url.split('#');
193 return (parts[0] + '&' + query).replace(/[&?]{1,2}/, '?') + (parts.length === 2 ? ('#' + parts[1]) : '');
194}
3function addParamToUrl(url, param) {
4 return url + (/\?/.test(url) ? '&' : '?') + param;
5}
147_getURLWithParams(url) {
148 const config = this._config;
149
150 const defaultURLParams = config.defaultURLParams || {};
151
152 const keys = Object.keys(defaultURLParams);
153
154 if (!keys.length) {
155 return url;
156 }
157
158 const queryString = keys
159 .map(key => {
160 return key + '=' + defaultURLParams[key];
161 })
162 .join('&');
163
164 return url + (url.indexOf('?') > -1 ? '&' : '?') + queryString;
165}
6export function appendQuery(url, query) {
7 if ( query === '' ) return url;
8 return (url + '&' + query).replace(/[&?]{1,2}/, '?');
9}
51export function addUrlQuery(url, query) {
52 const urlObj = new URL(url, '/');
53 const existQuery = qs.parse(urlObj.query ? urlObj.query.slice(1) : '');
54 const newQuery = Object.assign({}, existQuery, query);
55 urlObj.set('query', newQuery);
56 if (/^https?:\/\//i.test(url)) {
57 return urlObj.href;
58 }
59 const parts = [urlObj.pathname];
60 const queryKeys = Object.keys(urlObj.query);
61 if (queryKeys.length > 0) {
62 parts.push(queryKeys.map(key => `${key}=${urlObj.query[key]}`).join('&'));
63 }
64 return parts.join('?');
65}
24function appendQuery(url, query) {
25 url = url.split('#');
26 var hash = url[1];
27 url = url[0];
28 url += url.indexOf('?') >= 0 ? '&' : '?';
29 url += query + (hash ? '#' + hash : '');
30
31 return url;
32}
18function getURL(query, options) {
19 URL = "https://api.duckduckgo.com/?q="+encodeURIComponent(query)+"&format="+options.format+"&t="+encodeURIComponent(options.useragent)+"&pretty="+options.pretty+"&no_redirects="+options.no_redirects+"&no_html="+options.no_html+"&skip_disambig="+options.skip_disambig;
20 return URL;
21}

Related snippets