Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

คุณสมบัติ Navigator (Navigator Properties) ที่ใช้ในหน้าเว็บได้มีอะไรบ้าง

ออบเจกต์ 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
cookieEnabledBoolean: true หากเบราว์เซอร์เปิดใช้งาน Cookieทุกเบราว์เซอร์
onLineBoolean: true หากออนไลน์ (อาจไม่แม่นยำเสมอไป)ทุกเบราว์เซอร์
geolocationออบเจกต์ Geolocation API (ใช้ได้เฉพาะ HTTPS/localhost)Modern Browsers
permissionsPermissions API (สอบถามสถานะ Permission: camera, microphone, notifications, ฯลฯ)Modern Browsers
serviceWorkerServiceWorkerContainer (สำหรับ PWA, Offline, Push)Modern Browsers
storageStorageManager API (ประมาณการ Quota, Persistence)Modern Browsers
deviceMemoryประมาณการ RAM ของอุปกรณ์ (GB) - Privacy concern, บางเบราว์เซอร์ปิดChrome, Edge
hardwareConcurrencyจำนวน CPU Logical Coresทุกเบราว์เซอร์สมัยใหม่
maxTouchPointsจำนวนจุดสัมผัสสูงสุดที่รองรับTouch Devices
connectionNetwork Information API (effectiveType, downlink, rtt, saveData)Chrome, Edge, Samsung Internet
pdfViewerEnabledBoolean: มี PDF Viewer ในตัวหรือไม่Chrome, Firefox, Edge
webdriverBoolean: true หากถูกควบคุมโดย WebDriver (Selenium, Puppeteer) - ใช้ตรวจจับ BotModern 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