Run Different Function based on URL Path Containing Different Strings

Viewed 56

I'm working on an old site that's multilingual. All of the French content displays by taking the english URL and appending /fr/ to the URL.

Example:

English About Page = www.example.com/about French About Page www.examples.com/fr/about

I need to run some javascript on this website but I need different javascript based on whether the person is on English or French.

Here's what I've put together but I don't trust my Javascript knowledge enough to know if this will work:

if (window.location.href.indexOf("/fr/") > -1) {

  (function() {
    var s = document.createElement('script');
    var h = document.querySelector('head') || document.body;
    s.src = 'https://acsbapp.com/apps/app/dist/js/app.js';
    s.async = true;
    s.onload = function() {
      acsbJS.init({
        statementLink: 'FR',
        footerHtml: 'FR',
        hideMobile: false,
        hideTrigger: false,
        disableBgProcess: false,
        language: 'fr',
        position: 'left',
        leadColor: '#005596',
        triggerColor: '#005596',
        triggerRadius: '50%',
        triggerPositionX: 'left',
        triggerPositionY: 'bottom',
        triggerIcon: 'people',
        triggerSize: 'medium',
        triggerOffsetX: 20,
        triggerOffsetY: 20,
        mobile: {
          triggerSize: 'small',
          triggerPositionX: 'left',
          triggerPositionY: 'center',
          triggerOffsetX: 10,
          triggerOffsetY: 0,
          triggerRadius: '50%'
        }
      });
    };
    h.appendChild(s);
  })();

  else {

    (function() {
      var s = document.createElement('script');
      var h = document.querySelector('head') || document.body;
      s.src = 'https://acsbapp.com/apps/app/dist/js/app.js';
      s.async = true;
      s.onload = function() {
        acsbJS.init({
          statementLink: 'en',
          footerHtml: 'en',
          hideMobile: false,
          hideTrigger: false,
          disableBgProcess: false,
          language: 'en',
          position: 'left',
          leadColor: '#005596',
          triggerColor: '#005596',
          triggerRadius: '50%',
          triggerPositionX: 'left',
          triggerPositionY: 'bottom',
          triggerIcon: 'people',
          triggerSize: 'medium',
          triggerOffsetX: 20,
          triggerOffsetY: 20,
          mobile: {
            triggerSize: 'small',
            triggerPositionX: 'left',
            triggerPositionY: 'center',
            triggerOffsetX: 10,
            triggerOffsetY: 0,
            triggerRadius: '50%'
          }
        });
      };
      h.appendChild(s);
    })();


  }

}

I need to add the fixed code to this

!function(){var e=document.querySelector(".main-carousel");if(e){var t=new Flickity(e,{wrapAround:!0,prevNextButtons:!1,imagesLoaded:!0});document.querySelector(".rotator__prev-next--prev").addEventListener("click",function(e){t.previous()}),document.querySelector(".rotator__prev-next--next").addEventListener("click",function(){t.next()})}var n=document.querySelector(".skip-nav-btn"),o=document.querySelector("main");n.addEventListener("click",function(){o.focus()}),window.addEventListener("DOMContentLoaded",function(){var e=window.location.pathname.split("/"),t="/"+e.slice(1,e.length-1).join("/"),n=this.document.querySelector(".header-menu__item__link[href='"+t+"']");n&&n.classList.add("header-menu__item__link--active")});for(var d=document.querySelectorAll(".header-menu__item__link"),i=0;i<d.length;i++)d[i].addEventListener("touchstart",function(e){e.target.nextSibling&&e.target.nextSibling.classList.contains("dropdown")&&document.activeElement!=e.target&&window.innerWidth>=1024&&(e.preventDefault(),e.target.focus())});var r=document.querySelectorAll(".dropdown__item__link"),s=document.body;document.querySelector(".menu-toggle").addEventListener("click",function(){s.classList.contains("menu-expanded")?(s.classList.remove("menu-expanded"),s.classList.add("menu-closed")):(s.classList.add("menu-expanded"),s.classList.remove("menu-closed"))});for(i=0;i<r.length;i++)r[i].addEventListener("click",function(e){s.classList.remove("menu-expanded"),s.classList.add("menu-closed")}),r[i].addEventListener("focus",function(e){e.target.parentNode.parentNode.parentNode.classList.add("dropdown-item-focused")}),r[i].addEventListener("blur",function(e){e.target.parentNode.parentNode.parentNode.classList.remove("dropdown-item-focused")});window.addEventListener("resize",function(){s.classList.contains("menu-expanded")&&this.window.innerWidth>=1024&&(s.classList.remove("menu-expanded"),s.classList.add("menu-closed"))});var c=document.querySelector(".filter__button"),a=document.querySelector(".filter__select"),l=document.querySelectorAll(".filter__item"),u=document.querySelector(".products"),m=document.querySelectorAll(".products__item");a&&a.addEventListener("change",function(e){u.classList.add("products--loading"),setTimeout(function(){v(a.value,l[a.selectedIndex].innerHTML),u.classList.remove("products--loading")},200)});for(i=0;i<l.length;i++)l[i].addEventListener("click",function(e){u.classList.add("products--loading"),setTimeout(function(){v(e.target.value,e.target.innerHTML),u.classList.remove("products--loading")},200)}),l[i].addEventListener("touchstart",function(e){u.classList.add("products--loading"),setTimeout(function(){v(e.target.value,e.target.innerHTML),u.classList.remove("products--loading")},200)});function v(e,t){e=e.toUpperCase();for(var n=0;n<l.length;n++)l[n].innerHTML===t?l[n].classList.add("filter__item--active"):l[n].classList.remove("filter__item--active");if(c.innerHTML=t+"<svg"+c.innerHTML.split("<svg")[1],"ALL"===e)for(n=0;n<m.length;n++)m[n].classList.remove("products__item--hidden");else for(n=0;n<m.length;n++)m[n].getAttribute("data-category")==="products__item--"+e.toLowerCase()?m[n].classList.remove("products__item--hidden"):m[n].classList.add("products__item--hidden")}var L=document.querySelector(".anchor#main");function p(){var e=document.querySelector(".header").clientHeight,t=document.querySelector(".footer").clientHeight;document.body.style.paddingTop=e+"px",document.body.style.paddingBottom=t+"px",L.style.top=-e+"px"}p(),window.addEventListener("resize",p)}();

1 Answers

DRY: Don't repeat yourself

Also set the src AFTER the onload event handler

const lang = window.location.href.indexOf("/fr/") > -1 ? "fr" : "en";
const statementLink = lang === "fr" ? "fr.com" : "en.com";
const footerHTML = lang === "fr" ? "French123 " : "English123 ";
(function() {
  var s = document.createElement('script');
  var h = document.querySelector('head');
  s.onload = function() {
    acsbJS.init({
      statementLink: statementLink ,
      footerHtml: footerHTML ,
      hideMobile: false,
      hideTrigger: false,
      disableBgProcess: false,
      language: lang,
      position: 'left',
      leadColor: '#005596',
      triggerColor: '#005596',
      triggerRadius: '50%',
      triggerPositionX: 'left',
      triggerPositionY: 'bottom',
      triggerIcon: 'people',
      triggerSize: 'medium',
      triggerOffsetX: 20,
      triggerOffsetY: 20,
      mobile: {
        triggerSize: 'small',
        triggerPositionX: 'left',
        triggerPositionY: 'center',
        triggerOffsetX: 10,
        triggerOffsetY: 0,
        triggerRadius: '50%'
      }
    });
  };
  s.src = 'https://acsbapp.com/apps/app/dist/js/app.js';
  h.appendChild(s);
})();

Just add the code to after window.addEventListener("resize",p)}();

Related