【问题标题】:Check browser and language检查浏览器和语言
【发布时间】:2018-05-24 08:40:16
【问题描述】:

我有一个代码(如下所列),用于检查您的浏览器语言是否设置为“de”。如果您的浏览器是“de”,它会将您发送到地址“de.html”。如果不是“de”,您将被发送到“en.html”。这工作正常,但问题是:

这是一个 Webflow 网站,他们有一个编辑器,它是 url/?edit。前往该地址时,代码仍会尝试检查您的语言。它只是打印出 url/?editenenenenenenenenen.. 等等。有没有办法检查用户是否在 url/?edit 上,如果是,什么都不做?

   var lang = window.navigator.language;
   var userLang = window.navigator.userLanguage;
    if (lang == "de" || userLang == "de") {
      window.location.href = window.location.href + "de.html";  
    }
    else {
      window.location.href = window.location.href + "en.html";
    }

【问题讨论】:

    标签: javascript


    【解决方案1】:

    将另一个 if 语句包裹在您当前的 if 周围,如下所示:

    if (!window.location.href.includes('/?edit')) { ... }
    

    这将检查用户是否在url/?edit,如果是,则不执行任何操作。

    整体来说:

    var lang = window.navigator.language;
    var userLang = window.navigator.userLanguage;
    if (!window.location.href.includes('/?edit')) {
      if (lang == "de" || userLang == "de") {
        window.location.href = window.location.href + "de.html";
      } else {
        window.location.href = window.location.href + "en.html";
      }
    }
    

    【讨论】:

    • sigh.. 显然这在 IE 上不起作用.. 有什么想法吗?
    • @BillyBerg 到底什么不起作用?检测用户是否在“/?edit”或更改 url?开发控制台中是否有错误?
    • Nether 工作,你卡在空白页(有代码的那个)。
    • @BillyBerg 嗯,开发者控制台有错误吗?
    • 对不起.. 没有控制台。这个网站是用webflow创建的,没有控制台
    【解决方案2】:

    如果 URL 是 www.somedomain.com/editor?edit,那么您可以使用以下方法获取 ?edit 部分:

    window.location.search // '?edit'

    使用它:

    if(window.location.search === '?edit') return;

    或更宽松:

    if(window.location.search.includes('?edit')) return;

    【讨论】:

      猜你喜欢
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 2022-01-13
      • 2011-10-30
      • 2011-04-15
      • 2018-07-04
      相关资源
      最近更新 更多