Skip to content

Instantly share code, notes, and snippets.

@joshuapbritz
Created November 8, 2017 14:22
Show Gist options
  • Save joshuapbritz/648f752881a622a625cb83f540ad2c47 to your computer and use it in GitHub Desktop.
Save joshuapbritz/648f752881a622a625cb83f540ad2c47 to your computer and use it in GitHub Desktop.
A JavaScript function for constructing urls
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