Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 12, 2023 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/3e55719c45ce869bf833fb253bd6bcf0 to your computer and use it in GitHub Desktop.
Save code-boxx/3e55719c45ce869bf833fb253bd6bcf0 to your computer and use it in GitHub Desktop.
Javascript Get Full URL & URL Parts

JAVASCRIPT GET FULL URL & URL PARTS

https://code-boxx.com/url-parts-javascript/

IMAGES

baguette

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>Window Location</title>
<script>
// FULL URL - HTTP://SITE.COM/PATH/FILE.HTML?KEY=VALUE
console.log(location.href);
// PROTOCOL - HTTP:
console.log(location.protocol);
// HOST NAME - SITE.COM
console.log(location.hostname);
// HOST NAME + PORT - SITE.COM:8080
console.log(location.host);
// PROTOCOL + HOSTNAME + PORT - HTTP://SITE.COM:8080
console.log(location.origin);
// PORT - 8080
console.log(location.port);
// PATH + FILE - PATH/FILE.HTML
console.log(location.pathname);
// SEARCH QUERY - ?KEY=VALUE
console.log(location.search);
// HASH - #BOOKMARK
console.log(location.hash);
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Derived URL Parts</title>
<script>
// (A) FILE NAME ONLY
var fileonly = location.pathname;
// fileonly = fileonly.substring(fileonly.lastIndexOf("/")+1);
fileonly = fileonly.replace(/^.*[\\\/]/, "");
console.log(fileonly);
// (B) PATH ONLY
var pathonly = location.pathname;
pathonly = pathonly.substr(0, pathonly.lastIndexOf("/"));
console.log(pathonly);
// (C) BASE URL
var baseurl = `${location.protocol}//${location.host}${pathonly}/`;
console.log(baseurl);
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Query String</title>
<script>
// (A) NEW SEARCH PARAMS OBJECT
// ASSUMING THAT WE START WITH ?KEY=VAL&FOO=BAR
var params = new URLSearchParams(location.search);
// (B) HAS KEY & GET VALUE
console.log(params.has("key")); // true
console.log(params.get("key")); // val
// (C) LOOP THROUGH ALL KEYS
for (let k of params.keys()) {
console.log(k); // key, foo
}
// (D) APPEND, SET, DELETE
params.append("hello", "world"); // query string is now key=val&foo=bar&hello=world
params.set("hello", "kitty"); // query string is now key=val&foo=bar&hello=kitty
params.delete("key"); // query string is now foo=bar&hello=kitty
// (E) TO STRING
var str = params.toString();
console.log(str); // foo=bar&hello=kitty
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Parse URL From Elements</title>
</head>
<body>
<!-- (A) HTML ELEMENTS -->
<a id="demoA" href="https://code-boxx.com/path/file.html?key=val">Code Boxx</a>
<img id="demoB" src="baguette.png">
<script>
// (B) PARSE <A HREF>
let url = new URL(document.getElementById("demoA").href);
console.log(url.hostname); // code-boxx.com
console.log(url.pathname); // /path/file.html
// (C) PARSE <IMG SRC>
url = new URL(document.getElementById("demoB").src);
console.log(url.pathname); // /path/corgi.png
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment