简体   繁体   English

javascript window.location更新获取数据

[英]javascript window.location update a get data

I have a small javascript issue; 我有一个小javascript问题; I want to reload page with a selected language option value as a get variable. 我想使用选定的语言选项值作为get变量重新加载页面。 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: 如果我选择EN语言,则页面会用&lang = EN重新加载,我的问题是我使用concat,所以我得到了my_url&lang=EN&lang=FR&lang=SP ...因此,当我选择第一个EN然后是FR时,我不想获取my_url&lang=FR my_url&lang=EN&lang=FR我要替换lang变量,不仅要添加:

 <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. 数字7是子字符串的长度-lang = EN。

尝试

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

You could use the replace function: 您可以使用replace函数:

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 参考: http : //www.w3schools.com/jsref/jsref_replace.asp

If ?lang= exists, replace it with the new one. 如果?lang=存在,则用新的替换。 If not, just add the lang parameter. 如果没有,只需添加lang参数。

edit 编辑

I like the window.location.pathname solution from Dave Pile, this should be better than checking and replacing something. 我喜欢Dave Pile的window.location.pathname解决方案,这应该比检查和替换某些东西更好。

edit2 编辑2

 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. 查看此代码段,您必须更改loc以便它可以正常工作,还将var url更改为window.location并将elm更改为您的语言元素。

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. & ,则如果未设置lang,它将对其进行设置;如果未设置lang,它将对其进行替换。

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 . 在小提琴中,我使用的是div而不是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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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