首页 > 解决方案 > 检查浏览器和语言

问题描述

我有一个代码(如下所列),用于检查您的浏览器语言是否设置为“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

解决方案


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

window.location.search // '?edit'

使用它:

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

或更宽松地说:

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


推荐阅读