1

I'm trying to hide this class "mediad rh-flex-right-align" from my header only on HomePage. This class must appear on every other page.

I tried Javascript to achieve this, but it's not working so I probably made a mistake in these lines:

if (document.url == "https://www.comparer-acheter.fr/")
{
document.getElementsByClassName("mediad rh-flex-right-align")
[0].style.display = 'none';
}

Any suggestion is welcome to hide a CSS class on Homepage

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

5

There is no url attribute in document but there is a URL one.

So you can either use document.URL or window.location.href.

I've had problems with document.URL in the past so prefer the second one.

Edit : Note that this solution will not work if your URL contains a hash (ex: comparer-acheter.fr/#hash). It will also not work if the name of your website changes. If you want to check if you are on the homepage, you can use this :

if(window.location.pathname == '/') {
    // you are on the homepage
}

You will have more informations on how locations work on this post : javascript window location href without hash?

Ebatsin
  • 552
  • 5
  • 17
  • Thanks a lot! It's working perfectly. here the corrected code: if (window.location.href == "https://www.comparer-acheter.fr/") { document.getElementsByClassName("rh-flex-right-align") .[0].style.display = 'none'; –  May 18 '18 at 10:18
  • @Flo Be aware that is will not work if you have a hash in the URL (ex: "comparer-acheter.fr/#myHash"). If you just want to check if you are on the homepage, you can also use `window.location.pathname == '/'`. I updated my answer to reflect this. – Ebatsin May 18 '18 at 12:18