Tiny (≈ 1 kB gzipped) zero‑dependency URL parser & builder. Works everywhere from evergreen browsers to IE 11 (fallback via hidden <a> element if URL is missing).
<script src="urlparts.js"></script>- Native APIs (
location,URL) return a mix of properties,undefineds and browser‑specific quirks. - Heavyweights like URI.js or qs add 10–30 kB when all you need is «split URL into parts» and «assemble back».
- UrlParts does exactly that – no query‑builders, no routing, zero dependencies.
// parse a string → object
const obj = UrlParts.parse('https://u:p@ex.com:8080/a/b?x=1&y=2#top');
/* obj = {
protocol : 'https',
username : 'u',
password : 'p',
host : 'ex.com:8080',
hostname : 'ex.com',
port : '8080',
path : '/a/b',
query : { x:'1', y:'2' },
hash : 'top',
href : 'https://u:p@ex.com:8080/a/b?x=1&y=2#top'
}
*/
// build a string from parts
const url = UrlParts.build({
protocol:'https',
username:'u', password:'p',
hostname:'ex.com', port:8080,
path:'/a/b',
query:{ x:1, y:2 },
hash:'top'
});
// → "https://u:p@ex.com:8080/a/b?x=1&y=2#top"Returns an object with the following keys (missing parts are null):
| key | example | note |
|---|---|---|
protocol |
'https' |
without : |
username / password |
'u' / 'p' |
null if not present |
host |
'ex.com:8080' |
hostname + port |
hostname |
'ex.com' |
|
port |
'8080' |
|
path |
'/a/b' |
|
query |
{ x:'1', y:'2' } |
always plain object |
hash |
'top' |
without # |
href |
original string |
Creates a URL string. Provide only the parts you need; the rest will be omitted.
query– plain object → serialised withencodeURIComponent.hash– give value without leading#.
| function | description |
|---|---|
UrlParts.qParse(search) |
'?a=1&b=2' → { a:'1', b:'2' } |
UrlParts.qBuild(obj) |
{ a:1, b:2 } → 'a=1&b=2' (no leading ?) |
urlparts.js • 2.2 kB raw • ≈ 1.0 kB gzipped
MIT © 2025