根据不同访问设备跳转到PC页面或手机页面
目前很多网站都是采用了响应式自适应页面的设计了,根据访问设备的不同,显示不同的内容。但是还是会有一些节奏比较慢的网站,还是PC页面和手机PAD页面不同的访问域名。正好我这里有个需要,同一个域名要根据不同的访问设备显示PC页面或者手机页面,这里收集两个比较简洁的方法,都是通过JS代码实现的。
第一个:
<script type="text/javascript">
var userAgent = navigator.userAgent.toLowerCase();
var platform;
if(userAgent == null || userAgent == ''){
platform = 'WEB' ;
}else{
if(userAgent.indexOf("android") != -1 ){
platform = 'ANDROID';
location.href = "http://m.kuegou.com/";
}else if(userAgent.indexOf("ios") != -1 || userAgent.indexOf("iphone") != -1 || userAgent.indexOf("ipad") != -1){
platform = 'IOS';
location.href = "http://m.kuegou.com/";
}else if(userAgent.indexOf("windows phone") != -1 ){
platform = 'WP';
location.href = "http://m.kuegou.com/";
}else{
platform = 'WEB' ;
location.href = "http://www.kuegou.com/";
}
}
</script>
第二个:
这一个是两段代码,分别放到PC页面网页和手机页面网页,实现不同设备访问不同页面都能实现调整,比如电脑访问了手机页面的地址也会跳转到PC页面上来。
首先是放入PC页面的代码:
<script type="text/javascript">
var url = window.location.pathname;
var wapurl="http://3g.xxx.com"+url if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
if(window.location.href.indexOf("?mobile")<0){
try{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
window.location.href=wapurl;
}else{
window.location.href=wapurl;
}
}catch(e){}
}
}
</script>
下边是放入手机页面的代码:
<script type="text/javascript"> var url = window.location.pathname;
var pcurl="http://www.xxx.com"+url if(/AppleWebKit.*Mobile/i.test(navigator.userAgent)==false || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))==false){
if(window.location.href.indexOf("?mobile")<0){
try{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)==false){
window.location.href=pcurl;
} }catch(e){}
}
}
</script>
根据不同访问设备跳转到PC页面或手机页面的更多相关文章
- 用pc浏览器打开手机页面
原理是仿造http包头,下面已chrome为例. 方法:运行->输入如下手机操作系统对应的代码即可. 安卓: chrome.exe --user-agent="Mozilla/5.0 ...
- PC端和手机访问调用不同的页面,JS和PHP不同方法
js方法: var urlqr = location.href; urlqr = urlqr.replace(/iphoneF_/, "pc")这里写正则替换页面地址 if( ...
- Java判断访问设备为手机、微信、PC工具类
package com.lwj.util; import javax.servlet.http.HttpServletRequest; /** * 判断访问设备为PC或者手机--工具类 * * @de ...
- JS判断访问设备、客户端操作系统类型
先给出一个实例:判断windows.linux.android 复制以下代码另存为html文件即可. <html> <head> <title>判断操作系统< ...
- [转]JS判断访问设备、客户端操作系统类型
本文转自:http://www.cnblogs.com/duanguyuan/p/3534470.html 先给出一个实例:判断windows.linux.android 复制以下代码另存为html文 ...
- JS判断访问设备(userAgent)加载不同页面 JS判断客户端操作系统类型(platform)
//平台.设备和操作系统 var system ={ win : false, mac : false, xll : false }; //检测平台 var p = navigator.platfor ...
- Spring Mobile是如何判断访问设备的类型的
Spring最近换域名了,去转转,发现了一个有意思的项目:spring mobile. http://projects.spring.io/spring-mobile/ 这个项目有很多实用的功能,如识 ...
- 获取ip,获取客户端浏览器,获取客户端访问操作系统,获取客户端访问设备
/** * 获取ip */ public static function getIp() { if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CL ...
- JavaScript获取访问设备信息
<html xmlns=http://www.w3.org/1999/xhtml> <head> <title>JavaScript获取访问设备信息</tit ...
随机推荐
- FJNU Fang G and his Friends(状压DP)题解
Description 众所周知,fang G 有很多小伙伴,有一天,Fang G 打算带他们去玩有趣的游戏OOXX,这个游戏需要分成两组,有趣的是,每个人互相之间都有一个满意度,大家都想和自 ...
- spring事务的7种传播行为
https://blog.csdn.net/weixin_39625809/article/details/80707695 一般用于并发,分布式锁.复杂业务情况
- error——Fusion log——Debugging Assembly Loading Failures
原文 So...you're seeing a FileNotFoundException, FileLoadException, BadImageFormatException or you sus ...
- Python中不尽如人意的断言Assertion
Python Assert 为何不尽如人意 Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言失败则会抛出异常. >>> assert 1 + 1 ...
- python程序—系统检测
监控系统内存,CPU,硬盘的使用情况,到达阈值时会自动发送邮件报警! import yagmail import psutil def sendmail(subject,contents): #连接邮 ...
- 在CentOS6.9上Shell脚本定时释放内存cache
一.写Shell脚本 mkdir -p /var/script/ vim /var/script/freemem.sh 写入以下Shell脚本: #!/bin/bash # 当前已使用的内存大小 us ...
- linux下初始化mysql时报错
执行mysqld --initialize后报错 报错内容: 019-04-24 18:07:59 0 [Warning] TIMESTAMP with implicit DEFAULT value ...
- PyQt5——高级控件
PyQt5高级控件使用方法详见:https://blog.csdn.net/jia666666/article/list/4?t=1& PyQt5高级控件汇总: 1.QTableView 2. ...
- QT 右键弹出菜单
QWidget及其子类都可有右键菜单 1.设置标志 在widget初始化的时候 setContextMenuPolicy(Qt::CustomContextMenu); 设置为自定义菜单模式 2.在需 ...
- docker 中安装 rabbitMQ
安装rabbitMQ的命令 docker run -d --hostname my-rabbit --name rabbit -e RABBITMQ_DEFAULT_USER=admin -e RAB ...