开发者

Changing user locale doesn't work in chrome

i have a drop down list, that when a user click on language in it it calls a js function to change the locale it works fine in firefox but in chrome it doesn't work at all, i don't know why

list:

<select name="languageMenu" >
    <option selected="selected" ><fmt:message key="language.languages" /></option>
    <option value="en" onclick="switchLocale('en');"><fmt:message key="language.English"/></option>
    <option value="ar" onclick="switchLocale('ar');" ><fmt:message key="language.Arabic"/></option>
</select>

js:

<script type="text/javascript">

    function switchLocale(lang_opt) {
        var selected = lang_opt.selectedIndex;
        //var locale = lang_opt.options[selected].value;
        var locale = lang_opt;

        var href = document.location.href;
        if (href[href.length - 1] === "#") {
            href = href.substr(0, href.length - 1); // -1 removes "#"
        }
        var newHref = "";
        if (document.location.search.length > 0) {
            var langParamIndex = href.indexOf("lang");
            if (langParamIndex > -1) {
                newHref = href.substr(0, langParamIndex);
                newHref += "lang=" + locale;
                newHref += href.substr(langParamIndex + "lang=zz".length开发者_开发百科);
            } else {
                newHref = href + "&lang=" + locale;
            }
        } else {
            newHref = href + "?lang=" + locale;
        }
        document.location = newHref;
    }  
</script>


Using the onclick handler on the individual options isn't clearly defined behavior, you should change the event to the onchange on the select box itself:

<select name="languageMenu" onchange="switchLocale(this.value)"> 
    <option selected="selected"><fmt:message key="language.languages" /></option>
    <option value="en"><fmt:message key="language.English"/></option>
    <option value="ar"><fmt:message key="language.Arabic"/></option>
</select>

The options should also default to my current language so I'm not able to fire an onchange when I select the same locale as the one I'm using.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜