<script type="text/javascript"> //第一種方法(推薦使用) //new RegExp() 第二個參數爲修飾符(i執行對大小寫不敏感的匹配。) function isPc(){ var agents = ["android","windows phone","iphone","ipad","ipod","symbianos"]; for(var i=0; i<agents.length; i++){ if(new RegExp(agents[i],"i").test(window.navigator.userAgent)){//獲取代理頭信息 return false; } } return true; } if(isPc()){ console.info(window.navigator.userAgent); console.info("電腦哦"); } if(!isPc()){ console.info(window.navigator.userAgent); console.info("手機哦"); } //第二種方法 function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]; var flag = true; for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; } } return flag; } </script>