網站訪問者UA檢測及跳轉


/*!
* UA檢測及跳轉
*/
;(function(){
var whatdevice = {};
var myUA = window.navigator.userAgent.toLowerCase();
//正則得小寫

/*
* 是否移動設備:返回Boolean。
*/
whatdevice.isMobile = /(?:micromessenger|mobile|iphone|ipod|android|coolpad|mmp|smartphone|midp|wap|xoom|symbian|j2me|blackberry|windows phone|win ce)/.test(myUA);

/*
* 跳轉到手機網站:參數1是手機網站網址。
* 使用方法:whatdevice.go2mob('手機網站網址') 若不想跳轉,當前訪問的URL傳遞參數itsme=1,如http://www.hiry.cn/網站?itsme=1
*/
whatdevice.go2mob = function(){
var mobUrl = arguments[0]; //參數1
var myArg = [];
myArg = window.location.search.substr(1).match(/(^|&)itsme=([^&]*)(&|$)/i); //獲取URL參數itsme
var itsme = (myArg != null && myArg[2] != null)?myArg[2]:0;
if(itsme != 1 && mobUrl != null && whatdevice.isMobile){
window.location.href = mobUrl;
}
};

/*
* 跳轉到電腦網站:參數1是電腦網站網址。
* 使用方法:whatdevice.go2web('電腦網站網址') 若不想跳轉,當前訪問的URL傳遞參數itsme=1,如m.b5f.cn/?itsme=1
*/
whatdevice.go2web = function(){
var webUrl = arguments[0]; //參數1
var myArg = [];
myArg = window.location.search.substr(1).match(/(^|&)itsme=([^&]*)(&|$)/i); //獲取URL參數itsme
var itsme = (myArg != null && myArg[2] != null)?myArg[2]:0;
if(itsme != 1 && webUrl != null && !whatdevice.isMobile){
window.location.href = webUrl;
}
};

/*
* 是否蘋果手機:返回Boolean。
*/
whatdevice.isiOS = /(?:iphone)/.test(myUA);

/*
* 是否安卓手機:返回Boolean。
*/
whatdevice.isAndroid = /(?:android)/.test(myUA);

/*
* 是否微信瀏覽器:返回Boolean。
*/
whatdevice.isWechat = /(?:micromessenger)/.test(myUA);

/*
* 是否微信小程序:返回Boolean。
*/
whatdevice.isMiniProgram = window.__wxjs_environment === 'miniprogram';

/*注冊全局變量*/
window.whatdevice = whatdevice;
})();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM