Modify and extend a working jquery script

Viewed 25

I have this WORKING SCRIPT:

<script>
jQuery( document ).ready(function() {
    console.log( "ready!" );
var hideUrl=[
'https://www.gustotosto.it/shop/boni/',
'https://www.gustotosto.it/shop/agropic/',
];
if(hideUrl.indexOf(window.location.href)>=0){
    jQuery('.title-shop').hide();
}

});
</script>

Now I want to modify it, because it just hides the ".title-shop" for the urls listed in the var; I want to extend it and hide ".title-shop" if url contains the var hideUrl, subpages included. I modified it like this but it isn ot working. What is wrong? someone can help me?

<script>
jQuery( document ).ready(function() {
    console.log( "ready!" );
var hideUrl=[
'https://www.gustotosto.it/shop/boni/',
'https://www.gustotosto.it/shop/agropic/',
];
if (window.location.href.indexOf("hideUrl") > -1){
    jQuery('.title-shop').hide();
}
});
</script>
1 Answers

There's two issues in your code. Firstly hideUrl is a variable name, so you shouldn't wrap it in quotes. Doing that will mean it's treated as a string literal.

Secondly you need to loop through all the items in the hideUrl array and compare them individually. Try this:

jQuery($ => {
  var hideUrls = [
    'https://www.gustotosto.it/shop/boni/',
    'https://www.gustotosto.it/shop/agropic/'
  ];

  hideUrls.forEach(url => {
    if (window.location.href.indexOf(url) > -1) {
      $('.title-shop').hide();
    }
  });
});
Related