js获取网络设备类型

js获取网络设备类型

getNetworkType()

/***获取设备网络类型
 	@example: getNetworkType();
*/
export function getNetworkType (){
	var ua = navigator.userAgent;
       var networkStr = ua.match(/NetType\/\w+/) ? ua.match(/NetType\/\w+/)[0] : 'NetType/other';
       networkStr = networkStr.toLowerCase().replace('nettype/', '');
       var networkType;
       switch (networkStr) {
           case 'wifi': networkType = 'wifi'; break;
           case '4g': networkType = '4g'; break;
           case '3g': networkType = '3g'; break;
           case '3gnet': networkType = '3g'; break;
           case '2g': networkType = '2g'; break;
           default: networkType = 'other';
       }
       return networkType;
}

你可能感兴趣的:(#,javascript,javascript)