title: onclick function to change url - not working everytime
body: <p>I have four links each with an onclick event, each pass their language code to the changeLanguage function. The function should remove everything after the last '/' of the URL and replace it with the new language code and then redirect to the address. It works about 60/70% of the time, but if it doesn't the page reloads the current page. I've tried <code>window.location.href</code>, <code>window.location.replace</code>, timeouts. It seems random as well, it could happen changing from any country. Any ideas?</p>
  <p>e.g. from console:</p>
  <p>(On French page)</p>
  <pre><code>http://localhost:4000/product/fr
  </code></pre>
  <p>(change to Spanish - worked)</p>
  <pre><code>NEW URL http://localhost:4000/product/es
  
  Navigated to http://localhost:4000/product/es
  </code></pre>
  <p>(change to English - didn't work)</p>
  <pre><code>NEW URL http://localhost:4000/product/en
  
  Navigated to http://localhost:4000/product/es
  </code></pre>
  <p>HTML</p>
  <pre><code>  &lt;ul class=&quot;social-links&quot;&gt;
        &lt;li class=&quot;lang&quot;&gt;&lt;a id='en-lang-nav-button' onclick=&quot;return changeLanguage('en', event)&quot; &lt;%if(data.lang.lang == 'en'){%&gt; class=&quot;active&quot;&lt;%}%&gt;&gt;&lt;%=data.lang.links[0].text%&gt;&lt;/a&gt; | &lt;/li&gt;
  
        &lt;li class=&quot;lang&quot;&gt;&lt;a id='fr-lang-nav-button' onclick=&quot;return changeLanguage('fr', event)&quot; &lt;%if(data.lang.lang == 'fr'){%&gt; class=&quot;active&quot;&lt;%}%&gt;&gt;&lt;%=data.lang.links[1].text%&gt;&lt;/a&gt; | &lt;/li&gt;
  
        &lt;li class=&quot;lang&quot;&gt;&lt;a id='sp-lang-nav-button' onclick=&quot;return changeLanguage('es', event)&quot; &lt;%if(data.lang.lang == 'es'){%&gt; class=&quot;active&quot;&lt;%}%&gt;&gt;&lt;%=data.lang.links[2].text%&gt;&lt;/a&gt; | &lt;/li&gt;
  
        &lt;li class=&quot;lang&quot;&gt;&lt;a id='pt-lang-nav-button' onclick=&quot;return changeLanguage('pt', event)&quot; &lt;%if(data.lang.lang == 'pt'){%&gt; class=&quot;active&quot;&lt;%}%&gt;&gt;&lt;%=data.lang.links[3].text%&gt;&lt;/a&gt; &lt;/li&gt;
    &lt;/ul&gt;
  </code></pre>
  <p>JS</p>
  <pre><code>  function changeLanguage(lang, event) {
        var index, newURL;
        event.preventDefault();
        event.stopPropagation();
        index = window.location.href.lastIndexOf('/');
        newURL = window.location.href.substring(0, index) + '/' + lang;  
  
        console.log('NEW URL', newURL);
        window.location = newURL; 
        return false;
     };
  </code></pre>
  <p>Standard route in BE:</p>
  <pre><code>router.get('/about/:lang(en|fr|es|pt)?', function(req, res, next) {
      trackPath(req);
      res.render('about', { data: router.getTemplateData(req.params.lang, 'about') });
  });
  </code></pre>