JS 获取当前操作系统类型,浏览器,终端类型的工具

总结了一个JavaScript获取当前终端类型(pc, mobile),操作系统类型,浏览器类型,浏览器版本的小工具。

个人觉得还行,测试过没有问题,能识别ie7以及以上。

/**
	author: Weihuan
	date: 2017-6-26
**/
var _AgentInfo = {
	deviceType: "",  // pc or mobile
	OSname: "",			// windows, Android, linux and so on...
	browserName: "", 	//  chrome, safari, firefox, IE and so on...
	browserVer: "",   //  browser version, important if in IE environment.
	adaptType: 0,			// A type value, Adapt to the screen due to width
	_init: function(){
		_AgentInfo.setDeviceAndOS();
		_AgentInfo.setBrowser();
	},
	setDeviceAndOS: function(){
		var name = "unknown";
		if(window.navigator.userAgent.indexOf("Android") != -1){
			name = "Android";
		}else if(window.navigator.userAgent.indexOf("iPhone") != -1){
			name = "iPhone";
		}else if(window.navigator.userAgent.indexOf("SymbianOS") != -1){
			name = "SymbianOS";
		}else if(window.navigator.userAgent.indexOf("Windows Phone") != -1){
			name = "Windows Phone";
		}else if(window.navigator.userAgent.indexOf("iPad") != -1){
			name = "iPad";
		}else if(window.navigator.userAgent.indexOf("iPod") != -1){
			name = "iPod";
		}
		if(name != "unknown"){
			_AgentInfo.OSname = name;
			_AgentInfo.deviceType = "mobile";
			return;
		}
		if (window.navigator.userAgent.indexOf("Windows NT 10.0")!= -1){
			name="Windows 10";
		}else if (window.navigator.userAgent.indexOf("Windows NT 6.2") != -1){
	 		name="Windows 8";
		}else if (window.navigator.userAgent.indexOf("Windows NT 6.1") != -1){
			name="Windows 7";
		}else if (window.navigator.userAgent.indexOf("Windows NT 6.0") != -1){
			name="Windows Vista";
		}else if (window.navigator.userAgent.indexOf("Windows NT 5.1") != -1){
			name="Windows XP";
		}else if (window.navigator.userAgent.indexOf("Windows NT 5.0") != -1){
			name="Windows 2000";
		}else if (window.navigator.userAgent.indexOf("Mac") != -1){
			name="Mac/iOS";
		}else if (window.navigator.userAgent.indexOf("X11") != -1){
			name="UNIX";
		}else if (window.navigator.userAgent.indexOf("Linux") != -1){
			name="Linux";
		}
		_AgentInfo.OSname = name;
		_AgentInfo.deviceType = "pc";
	},
	setBrowser: function(){
		var nAgt = navigator.userAgent;
		var browserName  = navigator.appName;
		var fullVersion  = ‘‘+parseFloat(navigator.appVersion);
		var majorVersion = parseInt(navigator.appVersion,10);
		var nameOffset,verOffset,ix;
		if ((verOffset=nAgt.indexOf("Opera"))!=-1) { // In Opera, the true version is after "Opera" or after "Version"
		 browserName = "Opera";
		 fullVersion = nAgt.substring(verOffset+6);
		 if ((verOffset=nAgt.indexOf("Version"))!=-1)
			 fullVersion = nAgt.substring(verOffset+8);
		}
		else if ( (nAgt.indexOf("Trident"))!=-1 ) {   // ( ver >= ie7) In MSIE, the true version is after "MSIE" in userAgent
			if((verOffset=nAgt.indexOf("MSIE"))!=-1){
				fullVersion = nAgt.substring(verOffset+5);
			}else {
				fullVersion = ‘11.0‘;
			}
			if(fullVersion == 5){
				fullVersion = "11.0";
			}
			browserName = "IE";
		}
		else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {  // In Chrome, the true version is after "Chrome"
		 browserName = "Chrome";
		 fullVersion = nAgt.substring(verOffset+7);
		}
		else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {   // In Safari, the true version is after "Safari" or after "Version"
		 browserName = "Safari";
		 fullVersion = nAgt.substring(verOffset+7);
		 if ((verOffset=nAgt.indexOf("Version"))!=-1)
			 fullVersion = nAgt.substring(verOffset+8);
		}
		else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {    // In Firefox, the true version is after "Firefox"
		 browserName = "Firefox";
		 fullVersion = nAgt.substring(verOffset+8);
		}
		else if ( (nameOffset=nAgt.lastIndexOf(‘ ‘)+1) < (verOffset=nAgt.lastIndexOf(‘/‘)) ){   // In most other browsers, "name/version" is at the end of userAgent
		 browserName = nAgt.substring(nameOffset,verOffset);
		 fullVersion = nAgt.substring(verOffset+1);
		 if (browserName.toLowerCase()==browserName.toUpperCase()) {
			browserName = navigator.appName;
		 }
		}
		if ((ix=fullVersion.indexOf(";"))!=-1)		  // trim the fullVersion string at semicolon/space if present
			 fullVersion=fullVersion.substring(0,ix);
		if ((ix=fullVersion.indexOf(" "))!=-1)
			 fullVersion=fullVersion.substring(0,ix);
		majorVersion = parseInt(‘‘+fullVersion,10);
		if (isNaN(majorVersion)) {
		 fullVersion  = ‘‘+parseFloat(navigator.appVersion);
		 majorVersion = parseInt(navigator.appVersion,10);
		}
		_AgentInfo.browserName = browserName;
		_AgentInfo.browserVer = fullVersion;
	},
	isMobile: function(){
		if(_AgentInfo.deviceType == "mobile"){
			return true;
		}
		return false;
	},
	setAdaptType(){    	// A type value, Adapt to the screen due to width. For convenient
		if(screen.width <= 374){
			_AgentInfo.adaptType = 0;
		}else if(screen.width <= 413){
			_AgentInfo.adaptType = 1;
		}else {
			_AgentInfo.adaptType = 2;
		}
	}
}
_AgentInfo._init();

  

时间: 2024-11-07 13:47:05

JS 获取当前操作系统类型,浏览器,终端类型的工具的相关文章

原生JS获取DOM 节点到浏览器顶部的距离或者左侧的距离

关于js获取dom 节点到浏览器顶/左部的距离,Jquery里面有封装好的offset().top/offset().left,只到父级的顶/左部距离position().top/position().left: 原生写的话就是用获取节点,do while循环就可以了.代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta

JS获取到时间转换成字符串类型

JS获取到时间格式为:/Date(1479279579047)/ 提供一个函数可将其转换为"yyyy-MM-dd"和"yyyy-MM-dd hh:mm:ss" 1 //时间日期转换成string 2 function data_string(str, value) { 3 if (value == "yyyy-MM-dd hh:mm:ss") { 4 var d = eval('new ' + str.substr(1, str.length -

js获取页面元素距离浏览器工作区顶端的距离

先介绍几个属性:(暂时只测了IE和firefox,实际上我工作中用到的最多的是chrome) 网页被卷起来的高度/宽度(即浏览器滚动条滚动后隐藏的页面内容高度) (javascript)        document.documentElement.scrollTop //firefox (javascript)        document.documentElement.scrollLeft //firefox (javascript)        document.body.scro

JS获取当前使用的浏览器名字以及版本号

工作中需要通过JS去获取当前使用的浏览器的名字以及版本号,网上大堆资料都有一个关键词是 navigator.appName,但是这个方法获取的浏览器的名字只有两种要么是IE要么就是Netscap,倒是可以用来判断是否使用了IE,但是我想获取具体的浏览器产品名字比如  Firefox,Chrome等.所以只好通过navigator.userAgent,但是这个字符串是非常长的,分析他的特征,通过正则表达式来解决这个问题是不错的方法. (1) 获取浏览器名字+版本字符串 function getBr

js获取或判断任意数据类类型的通用方法(getDataType)和将NodeList转为数组(NodeListToArray)

function getDataType(any){ /* (1) Object.prototype.toString.call 方法判断类型: 优点:通用,返回"[object String]" 具体object的类型 缺点:不能返回继承的类型 (2)typeof x 缺点:对object类型不能细分: 优点:对空null的判断 'undefined'的应用; 返回类型有:'undefined' "string" 'number' 'boolean' 'funct

js获取客户端操作系统

function detectOS() { var sUserAgent = navigator.userAgent; var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows"); var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC

【转】通过js获取系统版本以及浏览器版本

1 function getOsInfo() { 2 var userAgent = navigator.userAgent.toLowerCase(); 3 var name = 'Unknown'; 4 var version = "Unknown"; 5 if(userAgent.indexOf("win") > -1) { 6 name = "Windows"; 7 if(userAgent.indexOf("window

JS获取网页窗口大小、浏览器窗口大小、页面元素位置

来自 阮一峰的网络日志>>http://www.ruanyifeng.com/blog/2009/09/find_element_s_position_using_javascript.html 一.两个概念 网页大小:一张网页的全部面积,就是它的大小.通常情况下,网页的大小由内容和CSS样式表决定. 浏览器窗口大小:浏览器窗口的大小,则是指在浏览器窗口中看到的那部分网页面积,又叫做viewport(视口). 很显然,如果网页的内容能够在浏览器窗口中全部显示(也就是不出现滚动条),那么网页的大

JS 获取客户端操作系统

function detectOS() {     var sUserAgent = navigator.userAgent;     var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");     var isMac = (navigator.platform == "Mac68K") || (navigator.platform ==