正则和三木
window.location.href = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ? "https://www.baidu.com/" : "http://news.baidu.com/";
eg2.
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <HEAD> 4 <TITLE> New Document </TITLE> 5 </HEAD> 6 7 <BODY> 8 <SCRIPT LANGUAGE="JavaScript"> 9 <!-- 10 function detectOS(){ 11 var sUserAgent = navigator.userAgent; 12 var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows"); 13 var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh"); 14 if(isMac) return "Mac"; 15 var isUnix = (navigator.platform == "X11") && !isWin && !isMac; 16 if(isUnix) return "Unix"; 17 var isLinux = (String(navigator.platform).indexOf("Linux") > -1); 18 if(isLinux) return "Linux"; 19 if(isWin) 20 { 21 var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1; 22 if(isWin2K) return "Win2000"; 23 var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1; 24 if(isWinXP) return "WinXP"; 25 var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1; 26 if(isWin2003) return "Win2003"; 27 } 28 return "None"; 29 } 30 31 alert(detectOS()) 32 33 //--> 34 </SCRIPT> 35 </BODY> 36 </HTML>
eg3.
1 function IsPC() { 2 var userAgentInfo = navigator.userAgent; 3 var Agents = ["Android", "iPhone", 4 "SymbianOS", "Windows Phone", 5 "iPad", "iPod"]; 6 var flag = true; 7 for (var v = 0; v < Agents.length; v++) { 8 if (userAgentInfo.indexOf(Agents[v]) > 0) { 9 flag = false; 10 break; 11 } 12 } 13 return flag; 14 }
eg4.
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=B页面; } } browserRedirect();