【问题标题】:Redirection if language of browser is not russian如果浏览器的语言不是俄语,则重定向
【发布时间】:2021-07-16 12:41:54
【问题描述】:

这是我最后一个问题的延伸。我在JS上写了一个函数

const uri = window.location;
const lang = window.navigator.language;
if( uri.href.indexOf('lang') < 0) {
    if (lang != 'ru-RU'){
        const re = "https://www.site/";
        const url = window.location.href;
        const newstr = url.replace(re, 'https://www.site/en/');
        window.location.href = newstr;
    }
}

但是当我从英文浏览器进入时,它只是保持重定向,几秒钟后链接开始看起来像这样:https://www.site/en/en/en/en/en/en/en/en/。 url只能更换一次吗?

【问题讨论】:

  • 你在uri中没有找到'lang'并且lang不是ru-RU,因此你不断地重定向。
  • 如果我从俄罗斯浏览器ru-RU进入站点,什么都不会发生,因为函数只会在lang != ru-RU时执行。
  • 是的,但如果你是英文用户,你不是俄罗斯用户,因此你会不断重定向。
  • @luk2302 我该如何解决这个问题?我没听明白:you do not find 'lang' in the uri and the lang is not ru-RU

标签: javascript url redirect hyperlink uri


【解决方案1】:
<script>

const uri = window.location;
const lang = window.navigator.language;
if( uri.href.indexOf('lang') < 0) {
    if (lang != 'ru-RU'){
eng = window.location.href.includes('https://www.fashiondance.in.ua/en/');
console.log(eng);
if (eng == false){
const re = "https://www.fashiondance.in.ua/";
const url = window.location.href;
const newstr = url.replace(re, 'https://www.fashiondance.in.ua/en/');
window.location.href = newstr;
}
}
}
</script>

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 2020-05-11
    • 2018-02-10
    • 2018-07-06
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多