On the site we implement the function of switching languages. To make the link, for example, /site.com/ en /index.html, we switched to /site.com/ en /index.html? I just can not understand the logic of extrusion and substitution. Now the code is:

$('.language-select li').click(function(){ var setLang = $('.language-select').data('location'), dataLangSelect = $(this).data('lang'); $('.language-select').data('location', dataLangSelect); $('.language-select li').removeClass('active'); $(this).toggleClass('active'); if($(this).hasClass('active')){ if(dataLangSelect == 'ua') { window.location = '/ua/'; } if(dataLangSelect == 'ru') { window.location = '/ru/'; } if(dataLangSelect == 'en') { window.location = '/en/'; } } }) 
  • on the fly or with page refresh? - Vasily Barbashev
  • The page will definitely be reloaded. - NeedHate

1 answer 1

Here is the answer:

 $('.language-select li').click(function(){ var setLang = $('.language-select').data('location'), dataLangSelect = $(this).data('lang'); $('.language-select').data('location', dataLangSelect); $('.language-select li').removeClass('active'); $(this).toggleClass('active'); if($(this).hasClass('active')){ var str = window.location.pathname, res = str.split("/"); res[1] = dataLangSelect; var newLink = res.join('/'); window.location = newLink; } })