trying to join the different parts of a url plus add a delimiter for ": " (colon space) delimiter that does not get added to the last array value which could change. This is being used for a web analytics data layer.
I tried using the below however, it doesn't work if I have an array that is less than 4 and will add the delimiter to the end of the last variable.
Input:
var pathArray = window.location.pathname.split( '/' );
var siteDomain = hostName; // returns the URL hostname of the current page.
var siteSection2 = pathArray[1]; /// returns the second-level directory path of the current page.
var siteSection3 = pathArray[2]; // returns the third-level directory path of the current page.
var siteSection4 = pathArray[3]; // returns the fourth-level directory path of the current page.
1st attempt - tried the below but it removes the last path (pathArray[3])
var pageNameString = [hostName, pathArray[1], pathArray[2], pathArray[3]];
var pageName = pageNameString.slice(0, -1).join(': ');
2nd attempt - still adds the delimiter to the end if path is less than 4
var pageNameString = [hostName, pathArray[1], pathArray[2], pathArray[3]];
var pageName = pageNameString.slice(0, 4).join(': ');
3rd attempt - still adds the delimiter to the end
var pageName = [hostName, pathArray[1], pathArray[2], pathArray[3]].join(': ');
Desired Output:
- When 1 path deep (hostName) it should return: "hostname"
- When 2 paths deep (pathArray[1]) it should return: "hostname: ScottTests"
- When 3 paths deep (pathArray[2]) it should return: "hostname: ScottTests: path3"
- When 4 paths deep (pathArray[3]) it should return: "hostname: ScottTests: path3: index.html"
any help will be greatly appreciated
thanks in advance, Scott