首页 > 语言 > JavaScript > 正文

分享两个手机访问pc网站自动跳转手机端网站代码

2024-05-06 14:46:27
字体:
来源:转载
供稿:网友

4G降临,移动网站已经一发不可收拾,pc端和移动端官网并存。如何让别人访问你的pc端的官网直接跳转到移动端的网站呢?各位看官,小二上代码!来了!

判断浏览器是否为手机端

<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.Vevb.com"+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){}}}})();</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;)? 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");	}</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';   } });             
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选