Created
November 8, 2017 14:22
-
-
Save joshuapbritz/648f752881a622a625cb83f540ad2c47 to your computer and use it in GitHub Desktop.
A JavaScript function for constructing urls
This file contains 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 urlMaker(origin, path) { | |
var pl = path.length - 1; | |
var pathname = path.charAt(pl) === '/' ? path : path + '/'; | |
pathname = pathname.charAt(0) === '/' ? pathname : '/' + pathname; | |
var url = origin + pathname; | |
return url; | |
} | |
//Usage | |
//1: | |
urlMaker(location.origin, location.pathname); | |
//returns [http://example.com/] | |
//2: | |
urlMaker('https://www.example.com', 'home'); | |
//returns [https://www.example.com/home/] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment