ออบเจกต์ navigator ใน JavaScript ให้ข้อมูลเกี่ยวกับเบราว์เซอร์และสภาพแวดล้อมของผู้ใช้ นี่คือคุณสมบัติ (Properties) หลักที่ใช้งานได้:
ตารางคุณสมบัติ Navigator หลัก
| Property | คำอธิบาย | รองรับ |
|---|---|---|
appCodeName | ชื่อรหัสของเบราว์เซอร์ (เช่น "Mozilla" สำหรับ Chrome, Firefox, Safari, Edge) | ทุกเบราว์เซอร์ |
appName | ชื่ออย่างเป็นทางการของเบราว์เซอร์ (เช่น "Netscape" - ค่าเดิมมาตรฐาน) | ทุกเบราว์เซอร์ |
appVersion | เวอร์ชันของเบราว์เซอร์ พร้อมข้อมูลเพิ่มเติม (Platform, Language, Engine) | ทุกเบราว์เซอร์ |
userAgent | สตริง User-Agent ทั้งหมด (ใช้สำหรับ Browser Detection - ไม่แนะนำให้ใช้แล้ว) | ทุกเบราว์เซอร์ |
platform | แพลตฟอร์มที่เบราว์เซอร์คอมไพล์ (เช่น "Win32", "MacIntel", "Linux x86_64", "iPhone") | ทุกเบราว์เซอร์ |
language | ภาษาที่ตั้งค่าในเบราว์เซอร์ (รหัส ISO 639-1, เช่น "en-US", "th-TH") | ทุกเบราว์เซอร์ |
languages | อาร์เรย์ของภาษาที่ผู้ใช้ตั้งค่าไว้ (เรียงตามลำดับความสำคัญ) | Modern Browsers |
cookieEnabled | Boolean: true หากเบราว์เซอร์เปิดใช้งาน Cookie | ทุกเบราว์เซอร์ |
onLine | Boolean: true หากออนไลน์ (อาจไม่แม่นยำเสมอไป) | ทุกเบราว์เซอร์ |
geolocation | ออบเจกต์ Geolocation API (ใช้ได้เฉพาะ HTTPS/localhost) | Modern Browsers |
permissions | Permissions API (สอบถามสถานะ Permission: camera, microphone, notifications, ฯลฯ) | Modern Browsers |
serviceWorker | ServiceWorkerContainer (สำหรับ PWA, Offline, Push) | Modern Browsers |
storage | StorageManager API (ประมาณการ Quota, Persistence) | Modern Browsers |
deviceMemory | ประมาณการ RAM ของอุปกรณ์ (GB) - Privacy concern, บางเบราว์เซอร์ปิด | Chrome, Edge |
hardwareConcurrency | จำนวน CPU Logical Cores | ทุกเบราว์เซอร์สมัยใหม่ |
maxTouchPoints | จำนวนจุดสัมผัสสูงสุดที่รองรับ | Touch Devices |
connection | Network Information API (effectiveType, downlink, rtt, saveData) | Chrome, Edge, Samsung Internet |
pdfViewerEnabled | Boolean: มี PDF Viewer ในตัวหรือไม่ | Chrome, Firefox, Edge |
webdriver | Boolean: true หากถูกควบคุมโดย WebDriver (Selenium, Puppeteer) - ใช้ตรวจจับ Bot | Modern Browsers |
คุณสมบัติที่เลิกใช้งานแล้ว (Deprecated / Non-standard)
mimeTypes[]- MIME Types ที่รองรับ (Netscape only, ใช้navigator.mimeTypesแทน)plugins[]- Plugins ที่ติดตั้ง (Netscape only, ใช้navigator.pluginsแทน, แต่ก็ Deprecated)javaEnabled()- Java Applet support (Removed in modern browsers)taintEnabled()- Data Tainting (Removed)
ตัวอย่างการใช้งาน
// ตรวจสอบภาษา
if (navigator.language.startsWith('th')) {
console.log('ผู้ใช้ภาษาไทย');
}
// ตรวจสอบออนไลน์
window.addEventListener('online', () => console.log('กลับมาออนไลน์แล้ว'));
window.addEventListener('offline', () => console.log('ออฟไลน์'));
// Feature Detection (แนะนำมากกว่า Browser Detection)
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition(pos => console.log(pos.coords));
}
// ตรวจสอบ Hardware Concurrency สำหรับ Web Workers
const workers = navigator.hardwareConcurrency || 2;
console.log(`ใช้ ${workers} Workers`);
ข้อควรระวัง
- User-Agent Spoofing: ผู้ใช้/บอทสามารถเปลี่ยน
userAgentได้ → อย่าใช้สำหรับ Security/Logic สำคัญ - Privacy: บราว์เซอร์สมัยใหม่จำกัดข้อมูล (เช่น
deviceMemory,plugins) เพื่อลด Fingerprinting - Feature Detection > Browser Detection: ใช้
'feature' in navigatorหรือ!!navigator.featureแทนการเช็ค userAgent