javascript - 检查浏览器和语言
问题描述
我有一个代码(如下所列),用于检查您的浏览器语言是否设置为“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";
}
解决方案
如果 URL 是www.somedomain.com/editor?edit
,那么您可以?edit
使用以下方式获取该部分:
window.location.search // '?edit'
使用它:
if(window.location.search === '?edit') return;
或更宽松地说:
if(window.location.search.includes('?edit')) return;
推荐阅读
- swift - 在tableview单元格中更改like按钮的颜色
- teradata - 在 teradata 中,您可以将 cte 插入表中吗?
- android - 如何在 android gradle.kts 中定义自定义源集?
- fpga - 如何使用 MyHDL 模块在 PYNQ Z1 上实现以下数学方程?
- django - 如何为我的 django datetimefield 设置默认小时
- php - 三个文件中的多帖子请求 Wordfpress
- android - Flutter Firebase 未正确初始化。您是否将“google-services.json”文件添加到项目中?
- django - 未在 ModelForm 上呈现脆皮表单
- image - 颤振:将CameraImage从相机插件转换为base64而不将图像存储在文件中(直接)
- java - 如何获取以 JSON 格式保存的对象并将其放入控制器中的列表或地图中