0

I have a switch statement which handles URLs. I'm looking to include "#xyz" at the end of the URLs without having to create new cases for each additional URL.

switch(window.location.href)
{
    case "mywebsite.com/blog":
    case "mywebsite.com/blog#intro":
    case "mywebsite.com/blog#sources":
    case "mywebsite.com/blog#contact":
        do something...
        break;
    case "mywebsite.com/home":
    case "mywebsite.com/blog#about":
    case "mywebsite.com/blog#email":
    case "mywebsite.com/blog#contact":
        do something...
        break;
}

Should ideally be:

switch(window.location.href)
{
    case "mywebsite.com/blog*":
        do something...
        break;
    case "mywebsite.com/home*":
        do something...
        break;
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
drowsy99
  • 33
  • 3
  • Check out [this answer](https://stackoverflow.com/q/2896626/1563833), the most upvoted answers there use a regular expression. – Wyck Feb 24 '22 at 21:30
  • Where are you getting currentURL from? There are ways to get just parts of it, like [location.pathname](https://developer.mozilla.org/en-US/docs/Web/API/Location/pathname) should give /blog or /home without the extra stuff. – James Feb 24 '22 at 21:32
  • Your first code block does something very different from your second (if it worked). Your first was catching "mywebsite.com/**blog**#about" in addition to "mywebsite.com/**home**" Your second would not catch "mywebsite.com/**blog**#about"... – Heretic Monkey Feb 24 '22 at 21:49

0 Answers0