Skip to content

Instantly share code, notes, and snippets.

@johan
Created October 19, 2012 00:17
Show Gist options
  • Save johan/3915545 to your computer and use it in GitHub Desktop.
Save johan/3915545 to your computer and use it in GitHub Desktop.
Resolves a relative url against a base_url using browser internals not exposed to you in API form.
function resolveURL(url, base_url) {
var doc = document
, old_base = doc.getElementsByTagName('base')[0]
, old_href = old_base && old_base.href
, doc_head = doc.head || doc.getElementsByTagName('head')[0]
, our_base = old_base || doc_head.appendChild(doc.createElement('base'))
, resolver = doc.createElement('a')
, resolved_url
;
our_base.href = base_url;
resolver.href = url;
resolved_url = resolver.href; // browser magic at work here
if (old_base) old_base.href = old_href;
else doc_head.removeChild(our_base);
return resolved_url;
}
@senseysensor
Copy link

doesn't work for me in IE8...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment