1. 思路
获取浏览器语言、页面名称、区分手机端与电脑
根据特定方式命名 html 文件,然后独立文件,重定向
eg:
- root
- gap.html gap
- index.html 、index_en.html 、 index_en_mobile ......
2. 核心代码
var lan = /cn/ig.test(navigator.language) ? '' : '_en'; var device = isPC() ? '' : '_mobile'; var urlHead = window.location.href.match(/[^/]w+.htm/ig)[0].split('.')[0]; window.location.href = urlHead + '/index' + lan + device + '.html'; function isPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]; var flag = true; for (var i = 0; i < Agents.length; i++) { if (userAgentInfo.indexOf(Agents[i]) > 0) { flag = false; break; } } return flag; }