'Show/Hide HTML by URL using JS if-else statement

I need to build out a JS if-else function with the objective of:

if my web page is a “county", I want to display an HTML list showing “cities” nested within the county. I have 93 counties each with their own list of cities. I must show cities specific only to the parent county.

else I want to show the list of 93 cities.

So, I'm curious how to tell JS that if URL ends with this slug, do this thing; else do the default thing.


This website has exactly what I'm looking to build - see the location icon in the top left. The first list is a county, and when clicking a county, the list then changes to another list of cities.

The data for the list looks like this and I imagine any of these data points can be used to trigger the JS but my guess is to use the URL slug. Then, in HTML, have all 93 lists separated with classes.


Or, is there an easier way to do this? would greatly appreciate some help.



Solution 1:[1]

You can use window.location to work with url i think what you're looking for is

window.location.pathname

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mahdiar Mransouri