Q:

javascript window.location update a get data

I have a small javascript issue; I want to reload page with a selected language option value as a get variable.
if I select EN language, the page reload with &lang=EN,
My problem is that I use concat so I get my_url&lang=EN&lang=FR&lang=SP ...
so when I select first EN then FR I want to get my_url&lang=FR not my_url&lang=EN&lang=FR
I want to replace the lang variable not only to add:
 <select onchange="javascript:handleSelect(this)">
 <option>DE</option>
 <option>EN</option>
 <option>FR</option>
 <option>SP</option>
 <option>NL</option>
 <option>HR</option>
 <option>PL</option>
 <option>CZ</option>
 </select>

    <script type="text/javascript">
    function handleSelect(elm)
    {
    window.location = window.location.href +"?lang="+elm.value;
    }
    </script>

A:

Try this:
function handleSelect(elm)
{
    var href = window.location.href;

    if (href.indexOf("lang") > -1)
    {
        href = href.replace(/(lang)=\w+((?=[&])|)/, "lang="+elm.value);
    }
    else 
    {
        var char = (href.indexOf("?") == -1 ? "?" : "&");

        href+= char + "lang=" + elm.value;
    }

    window.location.href = href;
}

It should work with any kind of url keeping the params.
Fiddle. In the fiddle I'm using a div instead of the window.location.

