Skip to content

Instantly share code, notes, and snippets.

@theredhead
Created September 24, 2019 08:02
Show Gist options
  • Save theredhead/40747275e51b0d7f6677a3618909cc66 to your computer and use it in GitHub Desktop.
Save theredhead/40747275e51b0d7f6677a3618909cc66 to your computer and use it in GitHub Desktop.
some utility to help building urls
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