JS judges whether the application running the current script is a mobile terminal or some other information. I do not encounter it very often in my work. My colleague asked me if I was asked, so I sorted out some knowledge points I found before. Come out for your reference. If something is wrong, please point it out, and I will change it in time (#^.^#).
window.navigator
Start with this property: window.navigator returns a reference to a navigator object that can be used to query some information about the application running the current script.
Common properties and methods: (If you want to know detailed properties and methods, please click MDN )
Navigator Object Properties
Attributes |
illustrate |
appCodeName |
Returns the current browser's internal "code" name, which may not be "correct". |
appName |
Returns the official name of the current browser, which may not be "correct". |
appVersion |
Returns the version number of the current browser, which may not be "correct". |
cookieEnabled |
Returns a boolean value indicating whether cookies are enabled in the current browser. |
platform |
Returns a string indicating the type of system platform currently in use. |
userAgent |
Returns the current browser's user agent string. |
Navigator Object Methods
method |
describe |
javaEnabled() |
Indicates whether the current browser has Java support enabled. |
vibrate() |
If the device supports vibration (mobile phone or other), trigger the device to vibrate. |
registerContentHandler |
Allows a website to register itself as a handler for content of a given MIME type. |
eg:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="test"></div>
<script>
txt = "<p>Browser code: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser version: " + navigator.appVersion + "</p>";
txt+= "<p>启用Cookies: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Hardware platform: " + navigator.platform + "</p>";
txt+= "<p>UserAgent: " + navigator.userAgent + "</p>";
txt+= "<p>User Agent Language: " + navigator.systemLanguage + "</p>";
document.getElementById("test").innerHTML=txt;
</script>
</body>
</html>
Common jump codes
<script type="text/javascript">
// borwserRedirect
(function browserRedirect(){
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match (/ ipad / i) == 'ipad';
var bIsIphone = 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) == 'web';
var bIsCE = sUserAgent.match(/windows ce/i) == 'windows ce';
var bIsWM = sUserAgent.match(/windows mobile/i) == 'windows mobile';
var bIsAndroid = sUserAgent.match (/ android / i) == 'android';
if(bIsIpad || bIsIphone || bIsMidp || bIsUc7 || bIsUc || bIsCE || bIsWM || bIsAndroid ){
window.location.href = 'Jump mobile URL';
}
})();
</script>
<script type="text/javascript">
<!--
// platform, device and operating system
var system = {
win: false,
mac: false,
xll: false,
ipad:false
};
//Detect 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);
system.ipad = (navigator.userAgent.match(/iPad/i) != null)?true:false;
//Jump statement, if it is accessed by mobile phone, it will automatically jump to the wap.baidu.com page
if (system.win || system.mac || system.xll||system.ipad) {
// something....
} else {
window.location.href = "PC-side URL";
}
-->
</script>
Tencent jump
<script type="text/javascript">
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|ZTE/.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="Mobile URL";
}else if(/iPad/i.test(navigator.userAgent)){
//window.location.href="pad URL"
}else{
window.location.href="PC-side URL"
}
}catch(e){}
}
}
}
</script>
<script type="text/javascript">
<!--
// platform, device and operating system
var system = {
win: false,
mac: false,
xll: false,
ipad:false
};
//Detect 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);
system.ipad = (navigator.userAgent.match(/iPad/i) != null)?true:false;
//Jump statement, if it is a mobile phone access, it will automatically jump to the mobile phone page
if (system.win || system.mac || system.xll||system.ipad) {
} else {
window.location.href = "PC Webpage";
}
-->
</script>
JS determines the browser client type (ipad, iphone, android)
<script type="text/javascript">
var bForcepc = fGetQuery("dv") == "pc";
function fBrowserRedirect(){
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){
var sUrl = location.href;
if(!bForcepc){
window.location.href = "Mobile URL";
}
}
if(bIsIphoneOs || bIsAndroid){
var sUrl = location.href;
if(!bForcepc){
window.location.href = "Mobile URL";
}
}
if(bIsMidp||bIsUc7||bIsUc||bIsCE||bIsWM){
var sUrl = location.href;
if(!bForcepc){
window.location.href = "";
}
}
}
function fGetQuery(name){//Get parameter value
var sUrl = window.location.search.substr(1);
var r = sUrl.match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)"));
return (r == null ? null : (r[2]));
}
function fShowVerBlock(){
if(bForcepc){
document.getElementByIdx_x("dv_block").style.display = "block";
}
else{
document.getElementByIdx_x("ad_block").style.display = "block";
}
}
fBrowserRedirect();
</script>
What is the test terminal
var u = navigator.userAgent;
var isAndroid = u.indexOf ('Android')> -1 || u.indexOf ('Adr')> -1; // android
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
alert('Is it Android:'+isAndroid);
alert('Is it iOS:'+isiOS);
Reference URL
https://developer.mozilla.org/zh-CN/docs/Web/API/Window/navigator
http://www.jb51.net/article/104661.htm