Created
September 24, 2019 08:02
-
-
Save theredhead/40747275e51b0d7f6677a3618909cc66 to your computer and use it in GitHub Desktop.
some utility to help building urls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function base_href() { | |
try { | |
const base = document | |
.getElementsByTagName('base') | |
.item(0) | |
.getAttribute('href'); | |
return base !== '/' ? base : null; | |
} catch (e) { | |
return null; | |
} | |
} | |
export function local_url(pathElements): string { | |
const loc = window.location; | |
let args = Array.isArray(pathElements) ? pathElements : [pathElements]; | |
args = [base_href(), ...args]; | |
const path = args.filter(i => (i || '').length > 0).join('/'); | |
return `${loc.protocol}://${loc.host}/${path}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment