Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created July 2, 2023 04:44
Show Gist options
  • Select an option

  • Save code-boxx/03ffc851a059c90d0b9f3a6f140bcf06 to your computer and use it in GitHub Desktop.

Select an option

Save code-boxx/03ffc851a059c90d0b9f3a6f140bcf06 to your computer and use it in GitHub Desktop.
Javascript Build Query String

JAVASCRIPT BUILD QUERY STRING

https://code-boxx.com/build-query-string-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>URLSearchParams Object</title>
<script>
// (A) URL SEARCH PARAMS OBJECT TO QUICKLY BUILD QUERY STRING
var query = new URLSearchParams({
name : "Jon Doe",
email : "jon@doe.com",
colors : JSON.stringify(["Red", "Green", "Blue"])
});
query.append("KEY", "VALUE"); // to append more data
// (B) CONVERT TO STRING, APPEND TO URL
var url = "http://site.com/page?" + query.toString();
console.log(url);
// (C) WHATEVER COMES NEXT
// window.location.href = url;
// fetch(url).then(res=>res.text()).then(txt=>console.log(txt));
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>URI.JS Library</title>
<!-- CHECK https://cdnjs.com/libraries/URI.js FOR LATEST VERSION -->
<!-- DOCS: https://medialize.github.io/URI.js/ -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.19.11/URI.min.js"></script>
<script>
// (A) TARGET URL
var uri = new URI("http://site.com/page");
// (B) SET PARAMETERS
uri.setSearch({
name : "Jon Doe",
email : "jon@doe.com",
colors : JSON.stringify(["Red", "Green", "Blue"])
});
uri.addSearch("KEY", "VALUE"); // to append more data
// (C) OUTPUT ENTIRE URL STRING
var url = uri.toString();
console.log(url);
// (D) WHATEVER COMES NEXT
// window.location.href = url;
// fetch(url).then(res=>res.text()).then(txt=>console.log(txt));
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Manual Query String</title>
<script>
// (A) TARGET URL
var url = encodeURI("http://site.com/mypage");
// (B) APPEND DATA TO QUERY STRING
// TAKE NOTE - ?KEY FOR FIRST PARAMETER, &KEY FOR MORE PARAMETERS
url += "?name=" + encodeURIComponent("Jon Doe");
url += "&email=" + encodeURIComponent("jon@doe.com");
url += "&KEY=" + encodeURIComponent("VALUE");
// (C) TO APPEND AN ARRAY - JSON ENCODE
var colors = ["Red", "Green", "Blue"];
url += "&colors=" + JSON.stringify(colors);
// (D) FULL URL
// http://site.com/my%20page?name=Jon%20Doe&email=jon%40doe.com&KEY=VALUE&colors=["Red","Green","Blue"]
console.log(url);
// (E) WHATEVER COMES NEXT
// window.location.href = url;
// fetch(url).then(res=>res.text()).then(txt=>console.log(txt));
</script>
</head>
<body></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment