Skip to content

Instantly share code, notes, and snippets.

@amitmbee
Created January 27, 2018 07:18
Show Gist options
  • Save amitmbee/507727bf71a69f5a237606ddbe9f23f2 to your computer and use it in GitHub Desktop.
Save amitmbee/507727bf71a69f5a237606ddbe9f23f2 to your computer and use it in GitHub Desktop.
getAbsoluteUrl - get the absolute URL when a string parameter is passed as a argument to the function
//works in browser only
var getAbsoluteUrl = (function() {
var a;
return function(url) {
if(!a) a = document.createElement('a');
a.href = url;
return a.href;
};
})();
// Usage
getAbsoluteUrl('/something'); // https://davidwalsh.name/something
//How it Works
//the href proprty of the Document Object Model handles all the useless stuff and gets you the absolute URL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment