Pepe Pepe - 1 month ago 6
jQuery Question

Change div/span content by url addition?

I dont find a solution, so maybe its not possible with jquery?

My plan:
I have some divs like this:

<div>
<span class="en">Hello</span>
<span class="de">Hallo</span>
</div>
<div>
<span class="en">Whats up?</span>
<span class="de">Wie geht´s?</span>
</div>


aso.

So, if my url is just www.domain.com/..., only the "en"-content should be shown. If the url is www.domain.com/de/..., only the "de"-content should be shown. Is this possible with jquery?

Answer

You could do something like this:

var url = window.location.href;  //Get URL

if (url.includes("/de/")) {    //If URL Contains /de/
  $('.en').hide();
} else {
  $('.de').hide();
}

EDIT:

If you don't want to use includes due to browser support (as FlatLander points out in his answer, includes is ES6), then you can use indexOf:

var url = window.location.href;  //Get URL

if (url.indexOf("/de/") !== -1) {    //If URL Contains /de/
  $('.en').hide();
} else {
  $('.de').hide();
}
Comments