-2

I need help with my "switch" code I want to validate the final string of a url to change a background by assigning a class and applying the corresponding CSS, it always returns the "default" case and does not apply to other cases with any URL.

var url_location = window.location.href;

switch (url_location) {
    case window.location.href.indexOf('?osf_portfolio_type=Bodega'):
        jQuery('.page-title-bar').addClass('propiedad-bodega');
        break;
    case window.location.href.indexOf('?osf_portfolio_type=Terreno'):
        jQuery('.page-title-bar').addClass('propiedad-terreno');
        break;
    case window.location.href.indexOf('?osf_portfolio_type=Oficina'):
        jQuery('.page-title-bar').addClass('propiedad-oficina');
        break;
    case window.location.href.indexOf('?osf_portfolio_type=Local+comercial'):
        jQuery('.page-title-bar').addClass('propiedad-local-comercial');
        break;
    default:
        jQuery('.page-title-bar').addClass('propiedades-site');
        break;
}

Thank you for your help!

AlonsoCT
  • 177
  • 1
  • 4
  • 18
  • 2
    That is not how switch case works. You can switch between different values, not complex logical statements. You can only do this using regular `if`s. – luk2302 Mar 03 '21 at 17:28
  • 1
    Does this answer your question? [How to get the value from the GET parameters?](https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters) – luk2302 Mar 03 '21 at 17:29
  • Separately, what would it even mean to compare the `href` of the location with the result of `indexOf`? – T.J. Crowder Mar 03 '21 at 17:31
  • Oh! thank you very much for the answers now I understand, not everything that is done with an if will always apply to the switch statement. – AlonsoCT Mar 03 '21 at 17:46
  • Basically only `if (something == value1) { } else if (something == value2) { } else if (something == value3) { }` is easily translateable into a switch case. – luk2302 Mar 03 '21 at 18:44

1 Answers1

0
if (window.location.href.indexOf("?osf_portfolio_type=Bodega") > -1) {
  jQuery('.page-title-bar').addClass('propiedad-bodega');
} 
else if (window.location.href.indexOf("?osf_portfolio_type=Terreno") > -1) {
   jQuery('.page-title-bar').addClass('propiedad-terreno');
  }
else if (window.location.href.indexOf("?osf_portfolio_type=Oficina") > -1) {
   jQuery('.page-title-bar').addClass('propiedad-oficina');
  }

else if (window.location.href.indexOf("?osf_portfolio_type=Local+comercial") > -1) {
jQuery('.page-title-bar').addClass('propiedad-local-comercial');
 }
 else{
jQuery('.page-title-bar').addClass('propiedades-site');
}
AlonsoCT
  • 177
  • 1
  • 4
  • 18
  • 1
    this technichally answers the question but the code is still an absolute mess, extracting the get parameter value for `osf_portfolio_type` and then doing a switch case around that instead of using `indexOf` repeatedly. This code e.g. breaks if the `osf_portfolio_type` becomes the second get parameter instead of the first one. – luk2302 Mar 03 '21 at 18:43