This article mainly shares with you the method of obtaining network type in js, mainly in the form of code. I hope it can help everyone.
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; }
The above is the detailed content of js implements method of obtaining network type. For more information, please follow other related articles on the PHP Chinese website!