蝙蝠岛资源网 Design By www.hbtsch.com
4G降临,移动网站已经一发不可收拾,pc端和移动端官网并存。如何让别人访问你的pc端的官网直接跳转到移动端的网站呢?各位看官,小二上代码!来了!
常用的访问pc自动跳转到移动页面的代码
(function() {
if (/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile|Opera Mini/i.test(navigator.userAgent)) {
var siteName = window.location.pathname;
if (url.indexOf("") < 0) {
try {
if (typeof siteName !== "undefined") {
window.location.href = "https://m.jb51.net" + siteName
}
} catch (e) {}
}
}
})();
访问移动端自动跳pc端的代码
;(function() {
var reWriteUrl = function(url) {
if (url) {
var Splits = url.split("/"),
siteName = window.location.pathname;
if (typeof siteName !== "undefined") {
return "https://www.jb51.net" + siteName
}
}
};
if (!/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile|Opera Mini/i.test(navigator.userAgent)) {
var url = window.location.href;
var pathname = window.location.pathname;
if (url.indexOf("") < 0) {
try {
window.location.href = reWriteUrl(url)
} catch(e) {}
}
}
})();
判断浏览器是否为手机端
<script type="text/javascript">
(function(){var reWriteUrl=function(url){if(url){var Splits=url.split("/"),siteName=window.location.pathname;if(typeof siteName!=="undefined"){return"https://m.jb51.net"+siteName}}};if(/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile/i.test(navigator.userAgent)){var url=window.location.href;var pathname=window.location.pathname;if(url.indexOf("")<0){try{window.location.href=reWriteUrl(url)}catch(e){}}}})();
</script>
判断浏览器是否为pc端,是就跳到pc页面
<script>
var browser1 = {
versions: function () {
var u = navigator.userAgent, app = navigator.appVersion;
return {//移动终端浏览器版本信息
trident: u.indexOf('Trident') > -1, //IE内核
presto: u.indexOf('Presto') > -1, //opera内核
webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
mobile: !!u.match(/AppleWebKit.*Mobile/i) || !!u.match(/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/), //是否为移动终端
ios: !!u.match(/\(i[^;]+;( U;)"m.","www.");//如果不是手机打开,则跳转到pc页面
alert("pc");
}
</script>
以下是补充可以参考
jQuery判断浏览器是移动端还是电脑端自动跳转
一个段小代码,同一个网站针对移动端查看和电脑端查看跳转不同的页面。
首先加载jQuery文件。
$(function(){
var MobileUA = (function() {
var ua = navigator.userAgent.toLowerCase();
var mua = {
IOS: /ipod|iphone|ipad/.test(ua), //iOS
IPHONE: /iphone/.test(ua), //iPhone
IPAD: /ipad/.test(ua), //iPad
ANDROID: /android/.test(ua), //Android Device
WINDOWS: /windows/.test(ua), //Windows Device
TOUCH_DEVICE: ('ontouchstart' in window) || /touch/.test(ua), //Touch Device
MOBILE: /mobile/.test(ua), //Mobile Device (iPad)
ANDROID_TABLET: false, //Android Tablet
WINDOWS_TABLET: false, //Windows Tablet
TABLET: false, //Tablet (iPad, Android, Windows)
SMART_PHONE: false //Smart Phone (iPhone, Android)
};
mua.ANDROID_TABLET = mua.ANDROID && !mua.MOBILE;
mua.WINDOWS_TABLET = mua.WINDOWS && /tablet/.test(ua);
mua.TABLET = mua.IPAD || mua.ANDROID_TABLET || mua.WINDOWS_TABLET;
mua.SMART_PHONE = mua.MOBILE && !mua.TABLET;
return mua;
}());
//SmartPhone
if (MobileUA.SMART_PHONE) {
// 移动端链接地址
document.location.href = 'http://www.aaa.com/wap/index.html';
}
});
需要手机端也网页的数据同步
复制代码 代码如下:
<script type="text/javascript">
(function(){var ua=navigator.userAgent.toLowerCase();var bIsIpad=ua.match(/ipad/i)=="ipad";var bIsIphoneOs=ua.match(/iphone os/i)=="iphone os";var bIsAndroid=ua.match(/android/i)=="android";var bIsWM=ua.match(/windows mobile/i)=="windows mobile";if(bIsIpad||bIsIphoneOs||bIsAndroid||bIsWM){window.location.href="http://m.jb51.net/android/game/826.html"}})();
</script>
第二种方法:
<SCRIPT LANGUAGE="JavaScript">
function mobile_device_detect(url)
{
var thisOS=navigator.platform;
var os=new Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows Phone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows CE","BlackBerry","IEMobile");
for(var i=0;i<os.length;i++)
{
if(thisOS.match(os[i]))
{
window.location=url;
}
}
//因为相当部分的手机系统不知道信息,这里是做临时性特殊辨认
if(navigator.platform.indexOf('iPad') != -1)
{
window.location=url;
}
//做这一部分是因为Android手机的内核也是Linux
//但是navigator.platform显示信息不尽相同情况繁多,因此从浏览器下手,即用navigator.appVersion信息做判断
var check = navigator.appVersion;
if( check.match(/linux/i) )
{
//X11是UC浏览器的平台 ,如果有其他特殊浏览器也可以附加上条件
if(check.match(/mobile/i) || check.match(/X11/i))
{
window.location=url;
}
}
//类in_array函数
Array.prototype.in_array = function(e)
{
for(i=0;i<this.length;i++)
{
if(this[i] == e)
return true;
}
return false;
}
}
mobile_device_detect("http://***.***.com");
</SCRIPT>
备注这里的
mobile_device_detect("http://***.***.com");//里面的地址填的就是您的移动端的网站地址呦。
标签:
手机端,自动跳转
蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com
暂无分享两个手机访问pc网站自动跳转手机端网站代码的评论...
更新日志
2025年11月07日
2025年11月07日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]