JavaScript split URL into parts

How to split URL Using JavaScript

You can get all window location key values using this window.location 

const paths =     window.location    
Location {    ancestorOrigins: DOMStringList,     href:     \"http://localhost:5000/new-url\",     origin:     \"http://localhost:5000\",     protocol:     \"http:\",     host:     \"localhost:5000\", …}    
ancestorOrigins: DOMStringList { length: 0}
assign: ƒ assign()
hash: \"\"
host: \"localhost:5000\"
hostname: \"localhost\"
href: \"http://localhost:5000/new-url\"
origin: \"http://localhost:5000\"
pathname: \"/new-url\"
port: \"5000\"
protocol: \"http:\"
reload: ƒ reload()
replace: ƒ replace()
search: \"\"

Get the full URL

var newURL =     window.location.protocol +     \"//\" + +     \"/\" +     window.location.pathname +    

If you need to break up up the


, for example, a URL like

you can split the string on “/” characters

var pathArray =     window.location.pathname.split(    '/');

Then access the different parts by the parts of the array, like

var secondLevelLocation = pathArray[    0];

To put that pathname back together, you can stitch together the array and put the “/”‘s back in:

var newPathname =     \"\";    
for (i = 0; i < pathArray.length; i++) {
  newPathname += \"/\";
  newPathname += pathArray[i];