当前位置:网站首页>JS judge the mobile terminal and PC terminal
JS judge the mobile terminal and PC terminal
2022-06-23 02:56:00 【User 1349575】
Navigator object :
navigator Object contains all the information about the browser , All browsers are supported .
navigator Object has a property of userAgent, This is a read-only string , The browser is declared for HTTP Value of the requested user agent header . So we can judge navigator.userAgent Whether it contains some values to judge . The following for userAgent Print value of .
Method 1 :
function IsPC(){
var userAgentInfo = navigator.userAgent;
var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }
}
return flag;
}
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
//alert(navigator.userAgent);
window.location.href ="iPhone.html";
} else if (/(Android)/i.test(navigator.userAgent)) {
//alert(navigator.userAgent);
window.location.href ="Android.html";
} else {
window.location.href ="pc.html";
};Method 2 :
var browser={
versions:function(){
var u = navigator.userAgent, app = navigator.appVersion;
return {// Mobile terminal browser version information
trident: u.indexOf('Trident') > -1, //IE kernel
presto: u.indexOf('Presto') > -1, //opera kernel
webKit: u.indexOf('AppleWebKit') > -1, // Apple 、 Google kernel
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, // Firefox kernel
mobile: !!u.match(/AppleWebKit.*Mobile.*/), // Is it a mobile terminal
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios terminal
android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android Terminal or uc browser
iPhone: u.indexOf('iPhone') > -1 , // Is it iPhone perhaps QQHD browser
iPad: u.indexOf('iPad') > -1, // whether iPad
webApp: u.indexOf('Safari') == -1, // whether web It should be programmed , No head and bottom
weixin: u.indexOf('MicroMessenger') > -1, // Wechat or not
qq: u.match(/\sQQ/i) == " qq" // whether QQ
};
}(),
language:(navigator.browserLanguage || navigator.language).toLowerCase()
}
if(browser.versions.mobile || browser.versions.ios || browser.versions.android ||
browser.versions.iPhone || browser.versions.iPad){
window.location = "http://m.abc.com";
}
Method 3 : Excerpt from im- qq
var os = function() {
var ua = navigator.userAgent,
isWindowsPhone = /(?:Windows Phone)/.test(ua),
isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
isAndroid = /(?:Android)/.test(ua),
isFireFox = /(?:Firefox)/.test(ua),
isChrome = /(?:Chrome|CriOS)/.test(ua),
isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
isPhone = /(?:iPhone)/.test(ua) && !isTablet,
isPc = !isPhone && !isAndroid && !isSymbian;
return {
isTablet: isTablet,
isPhone: isPhone,
isAndroid : isAndroid,
isPc : isPc
};
}();
if(os.isAndroid || os.isPhone){
alert("-----");
}
Method four :
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|ZTE/.test(navigator.userAgent))){
if(window.location.href.indexOf("?mobile")<0){
try{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
window.location.href=" Mobile page ";
}else if(/iPad/i.test(navigator.userAgent)){
window.location.href=" Tablet page ";
}else{
window.location.href=" Other mobile terminal pages "
}
}catch(e){}
}
}
Method five :
<script language="javascript">
// platform 、 Equipment and operating system
var system ={
win : false,
mac : false,
xll : false
};
// Detection platform
var p = navigator.platform;
system.win = p.indexOf("Win") == 0;
system.mac = p.indexOf("Mac") == 0;
system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
// Jump statements , If it is mobile access, it will automatically jump to abc.com page
if(system.win||system.mac||system.xll){
}else{
window.location.href="http://abc.com";
}
</script>Methods six :
<script language="javascript">
function is_mobile() {
var regex_match = /(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220)/i;
var u = navigator.userAgent;
if (null == u) {
return true;
}
var result = regex_match.exec(u);
if (null == result) {
return false
} else {
return true
}
}
if (is_mobile()) {
document.location.href= 'http://domain.com'; // modify http://domain.com Jump to the target page address for you
}
</script>
Method 7: Baidu webapp edition
<!--- Identify mobile phones or computers js Start --->
<script language="javascript">
(function(){
var res = GetRequest();
var par = res['index'];
if(par!='gfan'){
var ua=navigator.userAgent.toLowerCase();
var contains=function (a, b){
if(a.indexOf(b)!=-1){return true;}
};
// The following http://abc.com Change it to yours wap Mobile page address As my http://abc.com
var toMobileVertion = function(){
window.location.href = 'http://abc.com/'
}
if(contains(ua,"ipad")||(contains(ua,"rv:1.2.3.4"))||(contains(ua,"0.0.0.0"))||(contains(ua,"8.0.552.237"))){return false}
if((contains(ua,"android") && contains(ua,"mobile"))||(contains(ua,"android") && contains(ua,"mozilla")) ||(contains(ua,"android") && contains(ua,"opera"))
||contains(ua,"ucweb7")||contains(ua,"iphone")){toMobileVertion();}
}
})();
function GetRequest() {
var url = location.search; // obtain url in "?" The string after the sign
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
</script>
<!--- Identify mobile phones or computers js end --->边栏推荐
- Tencent cloud server CVM system tool configuration
- Goframe framework (RK boot): fast implementation of CSRF verification
- Problem thinking and analysis process
- How to gracefully solve the problem of platform font adaptation
- [SaaS examination certification] apaas_ Tencent Qianfan magic pen
- SetTimeout and setinterval execution time
- New uniapp+uniui background management uniuadmin
- Detailed explanation of label smoothing and implementation of pytorch tenorflow
- Understand one article: build an activity analysis system
- What is ISBN code and how to make it
猜你喜欢

6. template for integer and real number dichotomy

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027

How to store, manage and view family photos in an orderly manner?

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030

5. concept of ruler method

Xgboost principle

Xgboost Guide
What is sitelock? What is the function?

Vulnhub DC-5

8. greed
随机推荐
Learning record -- memo on constructors and attributes in C
What is sitelock? What is the function?
How to batch make decreasing serial number barcode
DNS Service Setup
Reinforcement learning series (IV) -policygradient example
Salesforce file (II) custom development fileUpload
Salesforce fileUpload (III) how to display uploaded images
Learning records - things inherited by subclass parent of C #
Optimization method of live weak network
[SaaS examination certification] apaas_ Tencent Qianfan magic pen
What is ISBN code and how to make it
SQLSERVER database restore stored procedure script
Transformation solution of digital intelligent supply chain platform for project management in engineering industry
Pytest common summary
Section 6: basic configuration I of spingboot
Precision loss problem
Goframe framework (RK boot): Based on cloud native environment, distinguish configuration files (config)
EDI project cases of customers in medical device industry
Tencent cloud server CVM system tool configuration
How to use pictures in Excel in PPT template