Use jQuery to detect whether a device can make telephone calls (supports “tel://” protocol)

前端 未结 4 2104
天命终不由人
天命终不由人 2020-11-28 14:37

In my website, I have several links like so:

218.111.2222

I want to use jQuery (or

相关标签:
4条回答
  • 2020-11-28 14:45

    I'm not sure about Android or BlackBerry, but iOS will automatically pick up telephone numbers and wrap them like so: <a href="tel:xxx">xxx</a>...so you could have a hidden <div> somewhere that contains a phone number like 1-800-555-5555, then, on page load do something like this:

    var isTelephone = $("a[href*='tel:']").length > 0;
    

    This may or may not be portable to other platforms, you'll have to try it out.

    0 讨论(0)
  • 2020-11-28 14:46

    Some limited solution can be a way to detect some pure phones. But not all possible:

    (function(a) {
    	window.isPhone = /\bi?Phone\b|(?=.*\bAndroid\b)(?=.*\bMobile\b)|(?=.*\bAndroid\b)(?=.*\bSD4930UR\b)/i.test(a);
    })(navigator.userAgent || navigator.vendor || window.opera);
    
    console.info('This device %s make phone calls', window.isPhone ? 'is originally intended to' : 'probably can\'t' );

    0 讨论(0)
  • 2020-11-28 15:00

    One might be able to find the first instance of an href with tel:// in it and post an ajax call. If it was successful it should have a readyState of 1 so do nothing. On failure, find all hrefs with tel:// and grab inner html and replace the a tag.

    This is more of a hypothesis and untested.

    Another thought is most browser have custom support for phone number formatted strings, If you place in a phone number you shouldn't have to create the a tag as it should be done automatically.

    0 讨论(0)
  • 2020-11-28 15:01

    I'm using the following to detect its PROBABLY a phone before enhancing a non link element that into a tel link:

    var probablyPhone = (
        (/iphone|android|ie|blackberry|fennec/).test
         (navigator.userAgent.toLowerCase())
         && 'ontouchstart' in document.documentElement
      );
    

    I exclude ipod|ipad because I know they are not phones

    The key point here is PROBABLY It will of course return true an some tablet devices, but this enough for my purposes on a non phone touch device the link would just fail anyway - I mostly just want to exclude desktop hence the touch event detect. I suppose also checking device width would narrow it down too but ultimately there is no guaranteed way to detect a mobile device that is capable of phone calls that I've found.

    0 讨论(0)
提交回复
热议问题