javascript substr window.location.href
<p>this is a very beginner question but I don't know javascript, I only wants implement a snipplet I found googling around.</p>

<p>The original code is:</p>

<pre>
<code>
var langcodes=["it", "es"];
var langCode = navigator.language || navigator.systemLanguage;
var lang = langCode.toLowerCase(); 
lang = lang.substr(0,2); 
var dest = window.location.href; 
for (i=langcodes.length-1;i >= 0; i--){
    if (lang==langcodes[i]){
        dest = dest.substr(0,dest.lastIndexOf('.')) + '-' + lang.substr(0,2) + dest.substr(dest.lastIndexOf('.'));
        window.location.replace ?window.location.replace(dest) :window.location=dest;
    }
}
</code></pre>

<p>if my browser language is it_IT, will replace</p>

<pre><code>    http://www.mysite.com/test.html </code></pre>

<p>to</p>

<pre><code>    http://www.mysite.com/test-it.html </code></pre>

<p>i'd like the new url was:</p>

<pre><code>    http://www.mysite.com/it/test.html </code></pre>

<p>I tried several ways with no luck.</p>

<p>Any help is appreciated.</p>

<p>Max</p>
