Skip to content

Instantly share code, notes, and snippets.

@petermac-
Last active September 12, 2015 19:14
Show Gist options
  • Save petermac-/d1be3063c0cda1d2224c to your computer and use it in GitHub Desktop.
Save petermac-/d1be3063c0cda1d2224c to your computer and use it in GitHub Desktop.
getAbsoluteUrl.js
var getAbsoluteUrl = (function() {
var a;
return function(url) {
if(!a) a = document.createElement('a');
a.href = url;
return a.href;
};
})();
// Usage
getAbsoluteUrl('/something'); // http://davidwalsh.name/something

Getting an absolute URL from a variable string isn't as easy as you think. There's the URL constructor but it can act up if you don't provide the required arguments (which sometimes you can't). The "burn" element href handles and URL nonsense for you, providing a reliable absolute URL in return.

From http://davidwalsh.name/essential-javascript-functions

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