1. 判断浏览器类型
浏览器判断使用的github开源项目current-device,下面是地址:
https://github.com/matthewhudson/current-device 在浏览器中可以使用下面地址进行浏览器引入,但是加载速度慢,不建议使用,我这里是直接下载本地。
<script src="https://unpkg.com/current-device/umd/current-device.min.js"></script>
2. 根据浏览器跳转到对应的页面

这是我的项目结构:

│  about_us.html
│ index.html
├─js
│ current-device.min.js
│ defalut.js
└─mobile
about_us.html
index.html

其实只要把移动端页面放在mobile目录下即可,然后需要在每个页面(PC端和移动端)引入current-device.min.js和defalut.js,然后浏览器访问时,执行defalut.js的方法进行跳转。

defalut.js内容如下:

// 判断浏览器类型,跳转到对应的页面
if (device.mobile() && !location.pathname.startsWith("/mobile")) {
window.location.href = location.protocol + "//" + location.host + "/mobile" + location.pathname + location.search;
} else if (device.windows() && location.pathname.startsWith("/mobile")) {
let pathname = location.pathname;
let pcpath = pathname.substring(("/mobile".length), pathname.length);
window.location.href = location.protocol + "//" + location.host + pcpath + location.search;
}

defalut.js主要是根据当前浏览器的类型来增加和删除子目录,在移动端时,我就判断路径是否以 “mobile” 开头,不是我就在中间插入 “/mobile” ,在PC端同样如此操作。

最新文章

  1. windows下用visual studio code 调试go代码
  2. xp系统下硬盘安装centos6.5
  3. ELK日志分析系统(转)
  4. tcp服务的测试程序开源
  5. #define和const的简单区别
  6. 基于excel9.h的excel处理
  7. treegrid.bootstrap使用说明
  8. 第8天:CSS制作导航栏
  9. lxml.etree.HTML(text) 解析HTML文档
  10. Netty 服务端启动过程
  11. 8. svg学习笔记-文本
  12. vue2.0自学教程(一):走进vue2.0大观园
  13. 2018-01-04 浅尝The Little Prover一书, 重逢Chez Scheme
  14. SpringBoot日记——ElasticSearch全文检索
  15. 如何使用List&lt;HashMap&lt;String, String&gt;&gt;详细讲解
  16. Hibernate的查询功能
  17. [LeetCode] 102. Binary Tree Level Order Traversal_Medium tag: BFS
  18. 第5题 查找字符串中的最长回文字符串---Manacher算法
  19. Mastering stack and heap for system reliability
  20. 【转载】Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍

热门文章

  1. SSM/SSH框架的MySQL 读写分离实现的一种简单方法
  2. Angular RxJs:针对异步数据流编程工具
  3. nginx启动报错nginx: [error] open() &quot;/usr/local/etc/nginx/logs/nginx.pid&quot; failed
  4. C primer plus 6 编程练习答案
  5. windows7安装.NET Framework 4.5.2 框架(迅雷下载链接)
  6. B样条曲线方程和C++实现
  7. MySql学习-3.命令脚本
  8. Eclipse 如何添加 更换字体(转载)
  9. P4735 最大异或和 /【模板】可持久化Trie
  10. 10maven依赖继承、统一版本/编码