Created
October 19, 2012 00:17
-
-
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.
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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
doesn't work for me in IE8...