
ตรวจสอบเครื่องที่เปิด web เป็นมือถือจริงๆ โดยใช้ mobile-detect.js
มีโจทย์คือ เว็บเราอยาก ทำปุ่มโทรออกจากเว็บ โดยให้ show ปุ่มเฉพาะเปิดเว็บจากบนมือถือจริงๆ เท่านั้น ผมใช้ mobile-detect.js ในการตรวจสอบว่าเป็น mobile จริง ถึงจะ show ปุ่มโทรออก install เพิ่ม Script ลงในไฟล์ที่ต้องการ
1 |
<script src="//cdn.jsdelivr.net/mobile-detect.js/1.3.6/mobile-detect.min.js"></script> |
การใช้งาน
1 2 3 4 5 6 7 8 9 10 11 12 |
var md = new MobileDetect(window.navigator.userAgent); var mobile = md.mobile() ; //detected phone or tablet type var phone = md.phone() ; //detected phone type var tablet = md.tablet() ; //detected tablet type var userAgent = md.userAgent() ; //detected user-agent var os = md.os() ; //detected operating system var isIphone = md.is('iPhone') ; //ใช่ iPhone ? var isBot = md.is('bot') ; // ใช่ bot ? var webkit = md.version('Webkit') ; var versionBuild = md.versionStr('Build') ; var isMatch = md.match('playstation|xbox') ; |
อ่านเพิ่ม Doc […]