Use jQuery to detect whether a device can make telephone calls (supports "tel://" protocol)
In my website, I have several links like so:
<a href="tel://+12181112222" class="call">218.111.2222</a>
I want to use jQuery (or other method) to determine whether the device supports making calls / using the tel:// protocol. Does such a method exist in the world?
I want to use some method for enabling or disabling the links, because when clicked on desktop we come to a page like "Firefox doesn't know how to open this address, because the protocol (tel) isn't associated with any program."
Currently, I am sniffing the user agent and detecting if it is a mobile device. But, is there a better/accurate way? Something like jQuery's $.support.xx
?
if ( (/iPhone|iPod|iPad|Android|BlackBerry/).test(navigator.userAgent) != true ){
$(".call").attr("href", "#");
}
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.
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.
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.