方便SEO自动跳转手机端JS代码

1、网站<head></head>加入canonical、alternate标签

<link rel="canonical" href="{
   
   {\Request::url()}}"/>
<link rel="alternate" media="only screen and (max-width: 640px)" href="https://m.xxxx.com/{
   
   {\Request::path() == '/' ? '' : \Request::path()}}">

2.1、js

  function browserRedirect() {
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";

if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
window.location.href = document.getElementsByTagName('link')[1].href;
} else {
// window.location.href = 'index.html';
}
}
browserRedirect(); 

2.2、js

if (window.location.toString().indexOf('pref=padindex') != -1) {} else {
						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|ZT E/
								.test(navigator.userAgent))) {
							if (window.location.href.indexOf("?mobile") < 0) {
								try {
									if (/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
										window.location.href = document.getElementsByTagName('link')[1].href;
									} else if (/iPad/i.test(navigator.userAgent)) {} else {}
								} catch (e) {}
							}
						}
					}

猜你喜欢

转载自blog.csdn.net/sinat_37390744/article/details/110110789