手机网站和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("?pc") < 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("?m") < 0) {

try {

window.location.href = reWriteUrl(url)

} catch(e) {}

}

}

})();

判断浏览器是否为手机端

(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("?pc")<0){try{window.location.href=reWriteUrl(url)}catch(e){}}}})();

判断浏览器是否为pc端,是就跳到pc页面

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;)? CPU.+Mac OS X/), //ios终端

android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器

iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器

iPad: u.indexOf('iPad') > -1, //是否iPad

webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部

};

} (),

language: (navigator.browserLanguage || navigator.language).toLowerCase()

}

if(browser1.versions.mobile==false&&browser1.versions.ios==false&&browser1.versions.android==false&&browser1.versions.iPhone==false &&browser1.versions.iPad==false){

//location.href = location.href.replace("m.","www.");//如果不是手机打开,则跳转到pc页面

alert("pc");

}

以下是补充可以参考

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';

}

});

需要手机端也网页的数据同步

(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"}})();

第二种方法:

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

{

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

{

if(this[i] == e)

return true;

}

return false;

}

}

mobile_device_detect("http://***.***.com");

备注这里的

mobile_device_detect("http://***.***.com");//里面的地址填的就是您的移动端的网站地址呦。


Copyright Liukaiweb.Com Rights Reserved.

 渝ICP备2021008408号-5