js -- 移动端pc端自动切换
2024-09-07 03:56:04
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端同样如此操作。
最新文章
- windows下用visual studio code 调试go代码
- xp系统下硬盘安装centos6.5
- ELK日志分析系统(转)
- tcp服务的测试程序开源
- #define和const的简单区别
- 基于excel9.h的excel处理
- treegrid.bootstrap使用说明
- 第8天:CSS制作导航栏
- lxml.etree.HTML(text) 解析HTML文档
- Netty 服务端启动过程
- 8. svg学习笔记-文本
- vue2.0自学教程(一):走进vue2.0大观园
- 2018-01-04 浅尝The Little Prover一书, 重逢Chez Scheme
- SpringBoot日记——ElasticSearch全文检索
- 如何使用List<;HashMap<;String, String>;>;详细讲解
- Hibernate的查询功能
- [LeetCode] 102. Binary Tree Level Order Traversal_Medium tag: BFS
- 第5题 查找字符串中的最长回文字符串---Manacher算法
- Mastering stack and heap for system reliability
- 【转载】Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍
热门文章
- SSM/SSH框架的MySQL 读写分离实现的一种简单方法
- Angular RxJs:针对异步数据流编程工具
- nginx启动报错nginx: [error] open() ";/usr/local/etc/nginx/logs/nginx.pid"; failed
- C primer plus 6 编程练习答案
- windows7安装.NET Framework 4.5.2 框架(迅雷下载链接)
- B样条曲线方程和C++实现
- MySql学习-3.命令脚本
- Eclipse 如何添加 更换字体(转载)
- P4735 最大异或和 /【模板】可持久化Trie
- 10maven依赖继承、统一版本/编码