惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
博客园 - 司徒正美
TaoSecurity Blog
TaoSecurity Blog
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
S
Secure Thoughts
博客园 - 三生石上(FineUI控件)
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
H
Help Net Security
博客园 - 叶小钗
爱范儿
爱范儿
GbyAI
GbyAI
I
Intezer
M
MIT News - Artificial intelligence
Latest news
Latest news
Schneier on Security
Schneier on Security
T
Tor Project blog
Simon Willison's Weblog
Simon Willison's Weblog
I
InfoQ
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
罗磊的独立博客
N
News and Events Feed by Topic
T
The Blog of Author Tim Ferriss
V2EX - 技术
V2EX - 技术
B
Blog
T
Tailwind CSS Blog
N
Netflix TechBlog - Medium
Security Latest
Security Latest
V
V2EX
F
Fortinet All Blogs
Forbes - Security
Forbes - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Hacker News
The Hacker News
Scott Helme
Scott Helme
P
Privacy International News Feed
P
Palo Alto Networks Blog
H
Heimdal Security Blog
C
Cisco Blogs
T
The Exploit Database - CXSecurity.com
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
W
WeLiveSecurity
L
LINUX DO - 最新话题

日常 on 打工人日志

Proxmox VE(PVE) 更新到最新版本 iStoreOS(旁路由)使用openclash实现dns劫持 异常流量分析:图片库服务黑客入侵 openwrt 硬盘扩容 搭建ip地址检索服务 通知:《打工人日报》迁移到独立板块 黑群晖最新安装教程 推荐一下 容器云资源 2010年的天涯神贴聊房价 如何礼貌回绝不合理的需求 github 国内代理访问下载 逆境和成长-2022年终总结 优雅的使用Conda管理python环境 shell功能脚本集合 Cloudflare Zero Trust 内网穿透 headscale 部署使用 羊了个羊小程序 破解通关 RocketMQ k8s部署 4主4从集群 linux服务器 删除空间却未释放 VSCode插件推荐=> Code Runner ant build.xml 编写 记录一次上门打散工 Ant中如何添加第三方jar包依赖 linux 网络测速 网心云挂机教程 | 轻松实现睡后收入~ Proxmox VE 在线扩容磁盘分区 centos7.9 网络配置 RocketMQ 安装和启动 安装 minIO Azure S3网关 logrotate 日志滚动的使用 安装配置 Terraform rsync 文件同步 linux系统开启root权限 163企业邮箱设置教程 2021年第50周记 自建服务器内网穿透 树莓派搭建k3s 优秀英语教材的选择
获取用户浏览器默认语言设置,自动判断跳转不同网站
2021-12-16 · via 日常 on 打工人日志

自动判断跳转不同网站

  1. 根据用户目前的浏览器配置语言进行显示
  2. 供语言切换按钮,用户自定义选择不同的语言显示

根据识别用户的浏览器语言,自动判断并跳转到相应的语言网页,让你的网站更加灵动。
以下需要将代码放在 HTML 的内即可,然后自行制作多语言页面。
代码如下:

 1<script type="text/javascript">
 2  //获取用户语言的顺序是
 3  //1.获取本地缓存里的内容
 4  //2.用户浏览器的语言设置
 5  //如果上面2个都没有获取到,就直接使用'en'作为用户选择的语言
 6  var language =
 7    localStorage.getItem("locale") ||
 8    window.navigator.language.toLowerCase() ||
 9    "en";
10  //把用户的语言写入缓存,供下次获取使用
11  localStorage.setItem("locale", language);
12  //判断用户的语言,跳转到不同的地方
13  if (language.indexOf("zh-") !== -1) {
14    window.location = "/zh-cn/index.html";
15  } else if (language.indexOf("en") !== -1) {
16    window.location = "/en/index.html";
17  } else {
18    //其它的都使用英文
19    window.location = "/en/index.html";
20  }
21</script>

核心代码
其实核心代码就是利用 navigator 的 language 属性

navigator.language

第二种解决方案

可以通过获取用户的 IP,然后把 IP 放到 IP 库里查询所在地,从而加载对应的资源,这样的方案回更加准确!有的第三方会直接返回所在国家的编码,比如 cn / en 等就更好了

但是这样的方案也有一个弊端:如果用户通过科学上网,全局模式下,会被认为属于美国 / 日本等等(看梯子的 IP 而定了),那么会导致访问非常慢;但是这种偏差,很多翻墙的人都是了解的,没人会故意用美国的 IP 访问国内的淘宝 / 百度等网站的,除非是忘记切换回来了;

IP 判断
市场上有很多 IP 判断的,拿 IP 倒是非常好做的一件事;比如我现在可以拿到用户访问本网站时候的 IP;

欢迎关注我的博客www.jobcher.com