Created
January 27, 2018 07:18
-
-
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
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
//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