JS判断访问设备(userAgent)加载不同页面 JS判断客户端操作系统类型(platform)来定义不同的字体表现

分类:Javascript| 发布:camnprbubuol| 查看: | 发表时间:2013/5/9

判断当前访问网站的设备是否是PC

//平台、设备和操作系统
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){
alert("PC访问");
}else{
alert("非PC访问");
}

JS判断访问设备(userAgent)加载不同页面。代码如下:

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();

JS判断客户端操作系统类型(platform)来定义不同的字体表现。代码如下:

// 更详细的探测当前客户端使用的操作系统
function detectOS() {
var sUserAgent = navigator.userAgent;
var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel"); if (isMac)
return "Mac";
var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
if (isUnix) return "Unix";
var isLinux = (String(navigator.platform).indexOf("Linux") > -1);
if (isLinux) return "Linux";
if (isWin) {
var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1;
if (isWin2K) return "Win2000";
var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1;
if (isWinXP) return "WinXP";
var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1;
if (isWin2003) return "Win2003";
var isWin2003 = sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1;
if (isWin2003) return "WinVista";
var isWin2003 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1;
if (isWin2003) return "Win7";
}
return "other";
}

在WIN7系统下和XP系统下的宋体显示是不一样的、有的时候容易撑破格子 

而且WIN7下面微软雅黑是系统自带的、效果很好! 

以下的代码可以方便我们判断不同的系统在使用不同系统里自带效果好的字体!或者其它一些东西! 

调用办法

<script>document.write(detectOS());</script>

各系统下最佳字体

Win7 and Vista

"Microsoft Yahei",Tahoma 

XP

Tahoma, Helvetica, Arial, 'SimSun', sans-serif 

MAC

"Helvetica Neue",Helvetica,"Hiragino Sans GB",Arial

淘宝上用的通用的系统字体 

Tahoma, Helvetica, Arial, 'Microsoft Yahei', sans-serif

via

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/javascript/js-judge-device-and-os.html