简体   繁体   中英

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>

You could use

var currAddress = window.location.href;
var indexOfLang = currAddress.indexOf('lang=');
var tempAddress = currAddress.substring(indexOfLang, indexOfLang+7);
currAddress = currAddress.replace(tempAddress,'lang='+elm.value);
window.location = currAddress;

The number 7 is the length of substring - lang=EN.

尝试

window.location = window.location.pathname +"?lang="+elm.value;

You could use the replace function:

window.location = window.location.href.match(/lang=/) ? window.location.replace( /lang=(.*){2}/, 'lang=' + elm.value ) : window.location.href + '?lang=' + elm.value;

Reference: http://www.w3schools.com/jsref/jsref_replace.asp

If ?lang= exists, replace it with the new one. If not, just add the lang parameter.

edit

I like the window.location.pathname solution from Dave Pile, this should be better than checking and replacing something.

edit2

 var loc = 'http://test.de/?foo=bar'; // window.location.href; var seperator = loc.match(/\\?/) ? '&' : '?'; var elm = 'DE'; var url = loc.match(/lang/) ? loc.replace(/lang=(.*){2}/, 'lang' + elm ) : loc + seperator + 'lang=' + elm; document.getElementById('result').innerHTML = url; 
 <div id="result"></div> 

Look at this snippet, you have to change the loc so it should work, also change var url to window.location and elm to your language element.

It checks if parameters exists and change the seperator from ? to & , than if no lang is set, it will set it or if a lang is set, it will replace it.

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM