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

ตรวจจับการรองรับ Click-to-Call (tel:) ใน JavaScript

โปรโตคอล tel: รองรับใน Mobile เกือบทุกตัว

  • iOS Safari
  • Android Browser/Chrome
  • Opera Mini/Mobile
  • Symbian, BlackBerry, Windows Phone

Feature Detection (แนะนำ)

// ทดสอบสร้าง Link ดู
var a = document.createElement('a');
a.href = 'tel:+1234567890';
var supportsTel = a.protocol === 'tel:';

// หรือ User Agent Sniffing (สำหรับ Legacy Fixes)
if (/HTC825/i.test(navigator.userAgent)) {
    $('a[href^="tel:"]').each(function() {
        this.href = this.href.replace('tel:', 'wtai://wp/mc;');
    });
}

ปัจจุบัน tel: ทำงานได้ทุก Mobile Browser ไม่ต้อง Detect แค่ใส่ href="tel:+66812345678" ก็พอ