2019年1月14日教程大全UA判断跳转代码,电脑手机跳转代码
网站根据访问客户端设备UA(PC和手机移动)自动判断跳转代码
- 以下代码是插入到手机模板中 <script type=”text/javascript”> var system = { win: false, mac: false, xll: false }; var p = navigator.platform; system.win = p.indexOf(“Win”) == 0; system.mac = p.indexOf(“Mac”) == 0; system.x11 = (p == “X11″) || (p.indexOf(“Linux”) == 0); if (system.win || system.mac || system.xll) { window.location.href = “填写你电脑的根跟网址”; } else {} </script> 2.以下是电脑跳转,插入到电脑模板的js文件中 var browser = { versions: function () { var u = navigator.userAgent, app = navigator.appVersion; return { trident: u.indexOf(‘Trident’) > -1, presto: u.indexOf(‘Presto’) > -1, webKit: u.indexOf(‘AppleWebKit’) > -1, gecko: u.indexOf(‘Gecko’) > -1 && u.indexOf(‘KHTML’) == -1, mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/), ios: !!u.match(/i[^;]+;( U;)? CPU.+Mac OS X/), android: u.indexOf(‘Android’) > -1 || u.indexOf(‘Linux’) > -1, iPhone: u.indexOf(‘iPhone’) > -1 || (u.indexOf(‘Mac’) > -1 && u.indexOf (‘Macintosh’) < 0), iPad: u.indexOf(‘iPad’) > -1, webApp: u.indexOf(‘Safari’) == -1 };} (),language: (navigator.browserLanguage || navigator.language).toLowerCase()} if(!browser.versions.iPad){ if (browser.versions.android || browser.versions.iPhone) { self.location = “填写你的手机站根网址”; }}
代码可直接复制存储为一个js文件。然后一定记得吧文件放到顶部,这样网页刚打开就先解析js跳转代码进行跳转。如果放到网站底部的话,他会加载完整个网页才解析跳转代码跳转,增加了网页加载时间,浪费服务器带宽。
下面再发一个跳转代码:
<script type=”text/javascript”>
UA = navigator.userAgent.toLowerCase();
url = window.location;
url = url.toString();
if ((UA.indexOf(‘iphone’) != -1 || UA.indexOf(‘mobile’) != -1 || UA.indexOf(‘android’) != -1 || UA.indexOf(‘ipad’) != -1 || UA.indexOf(‘windows ce’) != -1 || UA.indexOf(‘ipod’) != -1) && UA.indexOf(‘ipod’) == -1) {
if (url.indexOf(“m.”) < 0) {
if (url.indexOf(“www.”) < 0){
var index = url.indexOf(“http://”);
if (index < 0){
url = “m.” + url;
}else{
url = url.replace(“http://”, “http://m.”);
}
}else{
url = url.replace(“www.”, “m.”);
}
Go(url);
}
}
function Go(url) {
window.location = url;
}
</script>